CodeCannibal
CodeCannibal

Reputation: 344

Spring MVC custom validation messages

In my Spring MVC project I created a model-class like this:

public class LoginModel {

    @NotBlank 
    private String username;

    // .. getter and setter...
}

After submitting a form, a controller method will be called. That method could look like this:

@RequestMapping("/submitLogin")
    public String submitLogin(@ModelAttribute("LM") @Valid LoginModel lm, BindingResult result) throws UnexpectedException {

        if(result.hasErrors()){
             return "Login";
        } else {
            // do something...
        }
}

Then - back on the jsp, I used the <form:errors> tag to display the error:

<form:errors path="username"/>

An error message will be displayed - that works fine. But I want to define a message resource which should overwrite the default message.

Maybe something like this

<form:errors path="username" resource="error.login"/>

How can this be done?

Upvotes: 0

Views: 1049

Answers (1)

Nandu cg
Nandu cg

Reputation: 84

You can define your message in the model class like this:

@NotBlank(message = "Please enter your username.")
private String username;

Upvotes: 1

Related Questions