问题
i have this Class
public Class Employee
{
String name;
List<Address> listOfAddress;
}
public Class Address
{
String location;
String streetName;
}
in my JSP page i have filled like this
<s:textfield id="streetName" name="listOfAddress[%{listOfAddress.size()}].streetName" size="20" maxlength="24" cssStyle="width: 100px" />
each time i submit the page an object of time Address is added to the list therefore the size will increase by one. when i view the source HTML of the previous textField, its name looks like this listOfAddress[0].streetName , if i submit the JSP page after succefull addition, it will return to the same page and the name of this textfield will be listOfAddress[1].streetName if you view its HTML source
and like this i can add as many addresses as i want to same Employee object.
so far everything is OK. the problem is when i want to validate this field ican't because it is dynamic if i put this validation it will validate it the first time only.
<field name="listOfAddress[0].streetName">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message>sorry this field is required field</message>
</field-validator>
</field>
what i want is to make the index of the list "listOfAddress" dymanic according to the size of the list.
i don't know how to pass it dynamicall from the jsp
can i do something like this ?
<field name="listOfAddress[**${dynamic index value}**].streetName">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
**<param name="myIndex">${dynamic index value}</param>**
<message>sorry this field is required field</message>
</field-validator>
</field>
or pass the dynamic value to a custom validator ?
please help me, how to validate the list when the index is dynamic
回答1:
Use visitor validator. See http://struts.apache.org/2.x/docs/using-visitor-field-validator.html.
来源:https://stackoverflow.com/questions/12459784/dynamic-field-name-for-field-validator-in-struts2