使用actionerror以相同的形式在不同的atrtributes上显示不同的错误

时间:2015-08-08 05:16:08

标签: jsp struts2

这是我的jsp代码...

<div>
                <label for="customerCode"><span class="required">*</span>CustomerCode</label>
                <s:textfield name="customerCode"></s:textfield> 
                <s:if test="hasActionErrors()">
                    <s:actionerror/>
                </s:if>
            </div>
            <div>
                <label for="customerName"><span class="required">*</span>Customer Name</label> 
                <s:textfield name="customerName"></s:textfield>
                <s:if test="hasActionErrors()">
                    <s:actionerror />
                </s:if>
            </div>

现在,对于上面的代码,我编写了一个java文件代码片段,如下所示......

if(customerCode.isEmpty())
    {
        addActionError("Customer Code cannot be empty");
    }
    else if(alphaNumericCheck(customerCode)==1)
    {
        addActionError("Customer Code should be alphanumeric only");
    }

以及对customerName的验证如下....

if(customerName.isEmpty())
    {
        addActionError("Customer Name cannot be empty");
    }

现在,问题是在验证时,两个字段都显示相同的消息...... 我尝试过使用addFieldError()。但是,由于css限制,它不起作用.. 任何解决方案???

1 个答案:

答案 0 :(得分:0)

完全 fieldError用例。 使用

addFieldError("customerCode", "Customer Code should be alphanumeric only" );

<s:fieldError fieldName="customerCode" />

并为您的元素提供一个等于名称

的ID
相关问题