如何:Struts 2验证动态表单数组



我从头到尾读了一遍Struts2验证数组

,这是有意义的,但希望人(四元数)将解释如何

"重写上面的代码以指定字段的名称(带索引),在这种情况下您可以使用,您将使用addFieldError方法。有关这些标签的详细信息,请参阅http://struts.apache.org/2.3.1.2/docs/tag-reference.html"

这是我的:

<s:form action="saveOrUpdateAction" method="get">
    <s:token/>
    <table>
        <tr>
            <td> Fund </td>
            <td> Award Code </td>
        </tr>
        <s:iterator value="gfeListWithEmptyCode">
            <tr>
                <td> <s:property value="sfafund "/> </td>
                <td> <s:property value="awardcode"/>
                    <input type="text" name="codeArray">
                </td>
            </tr>
        </s:iterator>
        <s:token />
        <s:submit key="Submit2"/>
    </table>
</s:form>

Part of my Action:

public void validate()
{
    if (fund == null || fund.trim().length() != 5 )
    {
        System.out.println("testing+++++++++++++++++++1");
        addFieldError("fund","Fund requires 5 characters.");
    }
    if (code == null || code.trim().length() != 3 )
    {
        System.out.println("testing+++++++++++++++++++2");
        addFieldError("code","Fund requires 3 characters.");
    }

    if (gfeListWithEmptyCode !=null)
    {
        int index = 0;
        for (GiftFundEntity giftFundEntity : gfeListWithEmptyCode)
        {
            if ( codeArray[index]!=null && codeArray[index].length() < 3 )
            {
                System.out.println("testing+++++++++++++++++++3");
                // technically, this is not possible to do because it requires codeArray[index] and not a string.
                addFieldError("codeArray","Code requires 3 characters.");
                index++;
            }
        }
    }

    try
    {
        this.execute();
    } catch (Exception e)
    {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

在验证期间,由于索引没有列出codeArray,因此jsp页面上没有显示红色错误消息。我怎么让它工作?*请注意*数组是动态的。

我浏览了struts文档并搜索了stackoverflow,但我不知道如何做到这一点。

感谢您的宝贵时间。

答案是struts2将Action类中的codeArray变量视为Array:这没有文档记录。

最新更新