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

use actionerror to display different errors on different atrtributes in the same form

这是我的 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 限制,它不起作用.. 有什么解决办法吗???

正是 fieldError 用例。 使用

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

<s:fieldError fieldName="customerCode" />

并为您的元素提供一个与名称相同的 ID