struts2 中字段验证器的动态字段名称 [英] dynamic field name for field validator in struts2
问题描述
我有这个班
public Class Employee
{
String name;
List<Address> listOfAddress;
}
public Class Address
{
String location;
String streetName;
}
在我的 JSP 页面中,我是这样填写的
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" />
每次我提交页面时,都会将一个时间对象添加到列表中,因此大小将增加一.当我查看上一个textField的源HTML时,它的名字看起来像这样 listOfAddress[0].streetName ,如果我在添加成功后提交JSP页面,它会返回到同一个页面,这个textfield的名字会是listOfAddress[1].街道名称如果你查看它的 HTML 源代码
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
像这样,我可以向同一个 Employee 对象添加任意数量的地址.
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>
我想要的是根据列表的大小使列表listOfAddress"的索引动态化.
what i want is to make the index of the list "listOfAddress" dymanic according to the size of the list.
我不知道如何从jsp动态传递它
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
推荐答案
使用访问者验证器.请参阅 http://struts.apache.org/2.x/docs/using-visitor-field-validator.html.
Use visitor validator. See http://struts.apache.org/2.x/docs/using-visitor-field-validator.html.
这篇关于struts2 中字段验证器的动态字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!