论坛首页 Java企业应用论坛

[Help]: Struts2 List object validation

浏览 1358 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-24   最后修改:2010-01-16
Now I suffer a problem with s2' validation.

Here is my problem:

In my action,there is a property

List<Peson> persons;

now in jsp, I use iterator like below:
<s:iterator   value= "persons "   status= "stat ">
    <s:textfield   label= "%{ 'Person   '+#stat.index+ '   Name '} "  name= "%{ 'persons[ '+#stat.index+ '].name '} "   />
    <s:textfield   label= "%{ 'Person   '+#stat.index+ '   Age '} "   name= "%{ 'persons[ '+#stat.index+ '].age '} "   />
</s:iterator>

I want to validation user input while submit the form, “name" should not empty, and "age" must be number.

in my validation config file I write like this:
<validators>
    <field name="persons[].name">
        <field-validator type="requiredstring">
            <message key="requiredstring" />
        </field-validator>
    </field>
    <field name="persons[].age">
        <field-validator type="short">
            <message key="requiredshort" />
        </field-validator>
    </field>
</validators>

It seems doesn't work.


Besides that, I want to add error style to the filed which doesn't pass the validation. In S1,we can use
<html:text errorStyle="errorStyle" .../>
to achieve this task, but How to do it in S2?
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics