user1101608
user1101608

Reputation: 31

Spring MVC - Hibernate form:errors and bindingresult, jsp don't display error messages

I use Spring MVC 3, Spring 3 and Hibernate 2.5 . I want to validate my inputs but i can't make it work : nothing appear on the page.

My jsp

<form:form commandName="entrepriseSearch" action="/search" modelAttribute="entrepriseSearch" class="search" method="POST">
    <table class="search">
        <tbody>
            <tr>
                <td>
                    <form:input path="champ1"/>
                    <form:errors path="champ1"/>
                </td>
            </tr>
                        ... 
        </tfoot>
    </table>
</form:form>

My Controller

 @RequestMapping(value = "search", method = { POST })
public String search(@Valid @ModelAttribute("entrepriseSearch")  EntrepriseSearch entrepriseSearch, BindingResult bindingResult, Model model) {

    if (bindingResult.hasErrors()) {
        // bindingResult works 
        return "domain/domentreprise/showSearchForm";
    }

    return "/search";
}

My Bean

public class EntrepriseSearch extends SearchForm implements Serializable {

private static final long serialVersionUID = 1L;

private String champ1;

@MinMaxLength(min = 9,max=14, nullable = true)
public String getChamp1() {
    return champ1;
}

public void setChamp1(String champ1) {
    this.champ1= champ1;
} }

What am i missing ? The binding works but the errors message are not displayed. Thanks

Upvotes: 3

Views: 2338

Answers (2)

Ho&#224;ng Long
Ho&#224;ng Long

Reputation: 10848

You may want to add a message to report when the error happens:

@MinMaxLength(min = 9,max=14, nullable = true, message="Error here")
public String getChamp1() {
    return champ1;
}

P/s: You may want to try to print out the results of bindingResult.getFieldErrors() for debugging purpose.

Upvotes: 1

Josef Prochazka
Josef Prochazka

Reputation: 1283

Check , during debug , which message code has the field error. Than make sure that you have specified message source that can serve messages for error codes.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basenames="message ...

Upvotes: 0

Related Questions