Why aren't Spring validation errors displayed on my JSP page?

I'm using Spring 3.1.1.RELEASE. I'm having trouble getting error messages to display on my JSP. Here's the method I'm using in my controller ...

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest request,
                         final Organization organization,
                         final Model model,
                         final BindingResult result) 
    String nextPage = "organizations/add";
    m_orgValidator.validate(organization, result);
    if (!result.hasErrors())
        final boolean isUpdate = !StringUtils.isEmpty(organization.getId()); 
        // We need this clause to prevent detached entity errors.
        if (StringUtils.isEmpty(organization.getId()))
        }   // if
        final String msgKey = isUpdate ? "org.updated.successfully" : "org.added.successfully"; 
        final Object[] args = new Object[0];
                             MessageFormat.format(resourceBundle.getString(msgKey), args));

        nextPage = "landing";
    } else { 
        model.addAttribute(ORG_MODEL_NAME, organization);
    }   // if

    return new ModelAndView(nextPage);
}   // save

And here's my JSP …

    <c:url var="action" value="/organizations/save" />
    <form:form modelAttribute="org" method="post" action="${action}">
        <form:hidden path="id" />
            <legend>Upload Fields</legend>
            <p><form:errors path="*" cssClass="error" /></p>
                <form:label for="name" path="name">Name *</form:label><br/>
                <form:input path="name" type="text"/>

I've verified through debugging that my validator is setting errors properly (result.hasErrors() = true), but errors are not displaying properly on my page. Any ideas? Below is my validator class if that helps.

public class OrgValidator implements Validator

    private OrganizationService m_orgSvc;


    public boolean supports(Class<?> clazz)
        return Organization.class.isAssignableFrom(clazz);

    public void validate(final Object target,
                         final Errors errors)
        final Organization org = (Organization) target;

        if (org != null && !StringUtils.isEmpty(org.getEodbId()))
            final String eodbId = org.getEodbId();
            final Organization foundOrg = m_orgSvc.findByEodbId(eodbId);
            if ((org.getId() == null && foundOrg != null) ||
                (org.getId() != null && foundOrg != null && !org.getId().equals(foundOrg.getId())))
                errors.rejectValue("eodbId", "AlreadyExists.org.eodb.id");
            }   // if
        } else if (org == null || org != null && StringUtils.isEmpty(org.getEodbId())) { 
            errors.rejectValue("eodbId", "NotExists.org.eodb.id");   
        }   // if

        if (org == null || org.getOrganizationType() == null)
            errors.rejectValue("organizationType", "Invalid.org.type");
        }   // if

        if (org == null || org.getCountry() == null)
            errors.rejectValue("country", "Invalid.org.country");
        }   // if

        if (org == null || org.getState() == null)
            errors.rejectValue("state", "Invalid.org.state");
        }   // if



The reason is that the BindingResult gets bound as a request attribute with a name which ends with .organization and tries to retrieve it with this suffix, whereas your model attribute name is org and that is the reason why it is not able to find the errors for your model.

The fix is to name your model attribute organization and to use this name in the jsp also:

model.addAttribute("organization", organization);

<form:form modelAttribute="organization" method="post" action="${action}">
        <p><form:errors path="*" cssClass="error" /></p>

Need Your Help

Redirect_uri_mismatch when contacting Google APIs with OAuth2

cordova google-api oauth-2.0 sencha-touch-2

I'm creating a mobile app using Sencha Touch and PhoneGap. I want to be able to allow users to log into the app using their Google credentials but am hitting a brick wall with the initial request w...

Xcode 4.4 Lion inconsistent newline behavior in C++ with GCC vs LLVM

xcode standard-library llvm-clang libc++

I've been using the following bit of code for years to consume characters from cin up and including the next newline.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.