当前页面: 开发资料首页 → J2EE 专题 → 请问tapestry的for组件具体怎么用?
请问tapestry的for组件具体怎么用?
摘要: 请问tapestry的for组件具体怎么用?
请尽量把各个参数的作用说清楚些,谢谢
up
Loops over a collection of source values. May also emulate an element (like an Any component). If this component is placed in a Form, it will automatically store the collection in Hidden fields so that the structure of the page is preserved when the form is submitted even if the values in the source parameter have changed.
Examples
View list of customers
This example displays a list of customers:
<table cellspacing="10">
<tr>
<td>ID</td>
<td>Name</td>
<td>Level</td>
</tr>
<tr>
<td colspan="3">
</td>
</tr>
<tr jwcid="@For" source="ognl:customerList" value="ognl:customer" element="tr">
<td>
</td>
<td></td>
<td></td>
</tr>
</table>
Edit a list of customers
This examples allows the user to edit a list of customers. The 'keyExpression' parameter is optional. It tells the component to use the 'id' expression to obtain the primary key of the customers. The parameter is particularly useful when the Customer object is not Serializable, as only the primary key is stored in the hidden fields, rather than the full Customer record.
<table cellspacing="10">
<tr>
<td>ID</td>
<td>Name</td>
<td>Level</td>
</tr>
<tr>
<td colspan="3">
</td>
</tr>
<tr jwcid="@For" source="ognl:customerList" keyExpression="id"
<td></td>
<td></td>
<td> model="ognl:@com.mycorp.Customer@MEMBER_LEVEL_MODEL"/></td>
</tr>
</table>
Hints
Representing data as a string
If the For component is used in a form, it will automatically convert the provided data to strings and store it in hidden fields in order to ensure that the form is processed correctly when submitted.
If the data is in one of the basic types or if the data is serializable, Tapestry will automatically build string representation of the objects. If it is not serializable however, an ApplicationRuntimeException will be thrown with the message 'Could not find a strategy instance for class'.
There are several options to resolve this problem:
Store only the primary keys of your data objects
You can define the name of a property that contains the primary keys using the keyExpression parameter.
The second example shows that approach and uses the id property to represent the object.
Alternatively, you can implement the IPrimaryKeyConverter interface and use the converter parameter to define how the primary key is obtained from the object.
Make your data class Serializable.
Tapestry will automatically convert Serializable data into a string and store it in the form.
Define your own way to convert the class into a string. To do this, implement the SqueezeAdaptor interface and register your squeeze adaptor in the WEB-INF/hivemodule.xml file. Please refer to the documentation for more information.
Make the For component volatile by setting the volatile parameter to 'true'. This approach is discouraged, since the data will not be stored in the form as a result. That may cause a StaleLinkException to be thrown if the data changes between the form rendering and the form submission. To avoid this problem you must make sure that the data stays the same.
Either of the above options will work, but the first two are preferred.