开发者

Check Using JSTL regarding Spring Binding Error

I have been trying this for a while but 开发者_开发问答can't find the right solution.

I want to use JSTL to check if there is any binding errors (field error or global error) that happened in my Spring MVC 2.5.

I know I can use this code:

<p>
    <spring:hasBindErrors name="searchItems">
        An Error has occured
    </spring:hasBindErrors>
</p>

But I want to utilize JSTL to check for any errors.

I have tried this one using JSTL:

<c:if test="${not empty errors}">
    An Error has occured
</c:if>

But it seems that I cannot catch it correctly.

I need to used JSTL since there are other parts of the JSP that relies on the presence or absence of a binding errors.


As said

I want to utilize JSTL to check for any errors

Just use (It just works on Spring MVC 2.5 - Not portable for Spring MVC 3.0 although i suppose it is requestScope['bindingResult.<COMMAND_NAME_GOES_HERE>.allErrors'])

<c:if test="${not empty requestScope['org.springframework.validation.BindingResult.<COMMAND_NAME_GOES_HERE>'].allErrors}">
    An Error has occured!!!
</c:if>

Keep in mind default command name is The non-qualified command class name with The first letter lowercased. Notice bellow command name is pet

private PetValidator petValidator = new PetValidator();

@RequestMapping(method.RequestMethod.POST)
public void form(Pet command, BindingResult bindingResult) {
    if(petValidator.validate(command, bindingResult)) {
        // something goes wrong
    } else {
        // ok, go ahead
    }
}

So your form should looks like

<!--Spring MVC 3.0 form Taglib-->
<form:form modelAttribute="pet">

</form:form>
<!--Spring MVC 2.5 form Taglib-->
<form:form commandName="pet">

</form:form>

Unless you use @ModelAttribute

@RequestMapping(method.RequestMethod.POST)
public void form(@ModelAttribute("command") Pet command, BindingResult bindingResult) {
    // same approach shown above
}

This way, your form should looks like

<!--Spring MVC 3.0 form Taglib-->
<form:form modelAttribute="command">

</form:form>
<!--Spring MVC 2.5 form Taglib-->
<form:form commandName="command">

</form:form>


Something like this:

<spring:hasBindErrors name="userName">
    <c:set var="userNameHasError" value="true" />
</spring:hasBindErrors>

<c:choose>
    <c:when test="${userNameHasError}">
         <%-- Display username as textbox --%>
    </c:when>
    <c:otherwise>
         <%-- Display username as label --%>
    </c:otherwise>
</c:choose>

You can probably also put a setup the errors to catch all errors on the page (untested):

<spring:hasBindErrors name="*">
    <c:set var="userNameHasError" value="true" />
</spring:hasBindErrors>

Enjoy!


After playing around with <spring:hasBindErrors> tag, I found it had certain limitations:

  • It is useful only when there are errors.

  • org.springframework.validation.Errors object is only accessible inside the tag

What if just wanted to know if there are errors or not. If there are no errors, <spring:hasBindErrors> is rendered useless. After doing some research with my colleague, we printed out all the request attributes. Turns out there is an attribute called:

org.springframework.validation.BindingResult.command

The command object here is your form backing command object. As unintuitive as it may be named, this attribute holds a reference to our Errors object. Thus, this works:

${requestScope['org.springframework.validation.BindingResult.command'].errorCount}

and gives us a handle over the much sought after Errors object in JSTL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜