Search code examples
validationjspstrutsstruts-1

<logic:messagesPresent> tag in Struts1 not looping through multiple errors in Action Messages


I have a field in a bean that is failing 2 validations, as such 2 messages are being inserted in ActionMessages with the following command:

validationErrors.add("field1", new ActionMessage("Phone number is greater than 10 digits", false));
validationErrors.add("field1", new ActionMessage("Phone number has invalid characters", false));

Although I see the errors in the ActionMessages object (by setting a breakpoint in the debugger), only the first one gets displayed in my JSP, where I have:

<logic:messagesPresent message="true">
    <html:messages id="message" property="field1" message="true">
        <logic:present name="message">
            <c:out value="${message}"/>
        </logic:present>
    </html:messages>
</logic:messagesPresent>

Why is only the first message displayed, when <html:messages> should loop through all the messages where the property is "field1"?


Solution

  • I ended up figuring out my issue and it had to do with how I am creating the new ActionMessage.

    When you use:

    public ActionMessage(<error message>, false)
    

    While it allows you to display a literal value by using the <html:messages> tag in conjunction with either a <bean:write> or <c:out>, it won't iterate over multiple messages for a given property, why I don't know.

    I tested and found that if I use a resource bundle and create the ActionMessage with a standard:

    public ActionMessage(<key in resource bundle>)
    

    I am able to display multiple messages for a single property.

    Unfortunately, because I am using hibernate validator I don't want to use a resource bundle and struts to replace the values (would rather have the hibernate validator annotation replace values) and will likely just display a single message at a time for now.