dynamic field name for field validator in struts2

心不动则不痛 提交于 2019-12-12 02:34:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!