问题
I am pretty new in xforms. and this question seems to b very simple. I have an xform which contain two text boxes and a button control. I want that when a user submits the form, the values in the text box are stored in the xform model. How I can do this?
Code is given below, when i click on the button , values in the model element doesnot change.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:form="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="test.css" />
<script type="text/javascript" src="formfaces.js"></script>
<form:model>
<form:instance>
<data xmlns="">
<textbox1>0</textbox1>
<textbox2>1</textbox2>
<textbox3>2</textbox3>
<textbox4>3</textbox4>
</data>
</form:instance>
<form:submission action="Text.html" id="submission" method="put" replace="instance"/>
</form:model>
</head>
<body>
<table>
<tr>
<td>
<form:input ref="/data/textbox1">
<form:label>TextBox1</form:label>
</form:input>
</td><td>
<form:input ref="/data/textbox2">
<form:label>TextBox2</form:label>
</form:input>
</td></tr>
<tr><td><form:input ref="/data/textbox3">
<form:label>TextBox3</form:label>
</form:input>
</td><td><form:input ref="textbox4">
<form:label>TextBox4</form:label>
</form:input>
</td></tr>
</table>
<form:submit submission="submission"><form:label>Submit</form:label></form:submit>
</body>
</html>
回答1:
That should be taken care of automatically.
But if you are trying to display model changes on the screen immediately, make sure to add incremental="true"
attributes to the xforms input elements, so that each keystroke generates update events, and xforms output elements are refreshed as well.
added:
You seem to be submitting to an html. I am guessing it is the html containing the form itself. You don't need to call it like that to get the model updated. You typically call a script with a PUT submit to get it stored on the server. A PUT usually doesn't return something either, though it could return a copy of what you were PUT'ing.
I took your code a made two important changes to show the model is updated automatically. I copied your table of form inputs and renamed the inputs to outputs. I also added the incremental attribute on the inputs to get the outputs updated after each keystroke. You may also notice I added a bit of CSS, but that is just to make label and value of the outputs distinguishable:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:form="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="test.css" />
<script type="text/javascript" src="formfaces.js"></script>
<style type="text/css">
label {
display: block;
float: left;
clear: both;
text-align: right;
padding-right: 1em;
width : 110px;
}
</style>
<form:model>
<form:instance>
<data xmlns="">
<textbox1>0</textbox1>
<textbox2>1</textbox2>
<textbox3>2</textbox3>
<textbox4>3</textbox4>
</data>
</form:instance>
<form:submission action="test.xml" id="submission" method="put" replace="instance"/>
</form:model>
</head>
<body>
<table>
<tr>
<td>
<form:input ref="/data/textbox1" incremental="true">
<form:label>TextBox1</form:label>
</form:input>
</td><td>
<form:input ref="/data/textbox2" incremental="true">
<form:label>TextBox2</form:label>
</form:input>
</td></tr>
<tr><td><form:input ref="/data/textbox3" incremental="true">
<form:label>TextBox3</form:label>
</form:input>
</td><td><form:input ref="textbox4" incremental="true">
<form:label>TextBox4</form:label>
</form:input>
</td></tr>
</table>
<table>
<tr>
<td>
<form:output ref="/data/textbox1">
<form:label>TextBox1</form:label>
</form:output>
</td><td>
<form:output ref="/data/textbox2">
<form:label>TextBox2</form:label>
</form:output>
</td></tr>
<tr><td><form:output ref="/data/textbox3">
<form:label>TextBox3</form:label>
</form:output>
</td><td><form:output ref="textbox4">
<form:label>TextBox4</form:label>
</form:output>
</td></tr>
</table>
<form:submit submission="submission"><form:label>Submit</form:label></form:submit>
</body>
</html>
If you replace the value of the submission action attribute with a reference to some .asp script, it will get called, and will receive the entire instance in the request body. Make sure to return a complete instance, as it will be replaced.
HTH!
来源:https://stackoverflow.com/questions/9737194/how-to-save-user-entered-values-in-xforms-textbox