Wednesday, December 20, 2006

Spring: Error message not displayed in JSP after calling errors.rejectValue

I spent about half an hour trying to debug a simple (seemingly stupid) error today.
I had a controller doing some validation and rejecting a value that it didn't like, as follows:

errors.rejectValue("file", "Attachments.file", args, "File");

In the JSP I had the following boilerplate code to display these errors:

<spring:bind path="Attachments.*">
<b>Got some errors? ${status.errorMessages}</b>
<c:if test="${not empty status.errorMessages}">
<div class="error"><c:forEach var="error"
<img src="<c:url value="/images/iconWarning.gif"/>"
alt="<fmt:message key="icon.warning"/>" class="icon" />
<c:out value="${error}" escapeXml="false" />
<br />


no matter what I did, those errors wouldn't display on the JSP!

. Finally I figured that lines <spring:bind path="Attachments.*">....</spring:bind> must be inside the <body> tag of the JSP. Now don't ask me why, but <spring:bind> tag won't get evaluated unless it's in the body of the JSP. Just moved it in the body and poof! it worked :).

This page is powered by Blogger. Isn't yours?