tags:

views:

51

answers:

1

Hi All,

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.

Any advise please? Thanks

+1  A: 

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>
Arthur Ronald F D Garcia
@Arthur. Thanks, I will try what you have suggested and let you know after if this is succssful
Mark Estrada
@Mark Estrada Ok, go ahead!
Arthur Ronald F D Garcia