0

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?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
CodeCannibal
  • 324
  • 6
  • 21
  • 5
    You can read this post http://stackoverflow.com/a/9244317/2055854 – Orest Feb 02 '16 at 12:48
  • Possible duplicate of [Use custom validation messages in Hibernate + Spring](http://stackoverflow.com/questions/9232284/use-custom-validation-messages-in-hibernate-spring) – riddle_me_this Feb 02 '16 at 15:58
  • This [tutorial](http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/) might help, too – MyBrainHurts Feb 02 '16 at 16:02

1 Answers1

1

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

@NotBlank(message = "Please enter your username.")
private String username;
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Nandu cg
  • 74
  • 1
  • 10
  • @CodeCannibal Will certainly do, but I'd rather go for i18n-messages and JSR-303 bean validation like in the comment by Orest, as this example here is hard to maintain in the long run. – MyBrainHurts Feb 02 '16 at 12:54
  • you can add your messages in a property file.and configure the file in the xml file.the property file is like this NotBlank.LoginModel .username= please enter your username – Nandu cg Feb 02 '16 at 12:57
  • That's right of course. But again it's easier to leave that to the already build in mechanism. Look at how easy it is to define a custom message for `username` while still having a default custom message for `@NotBlank`. – MyBrainHurts Feb 02 '16 at 13:05
  • ok. i use the above two methods for printing error messages.what is your expectation? tell me clearly. – Nandu cg Feb 02 '16 at 13:10
  • I tried Orest's solution and it works- but only for the validation tag itself. So "NotBlank=This may not be blank" works, but "NotBlank.loginModel.username=This may also not be blank" does not. Instead the default message is shown. I tried the hole path ("NotBlank.my.package.loginModel.username") but it does not change anything. What am I doing wrong here? – CodeCannibal Feb 02 '16 at 16:06
  • 1
    @CodeCannibal Put `System.out.println("\n" + result.getFieldErrors() + "\n");` in your controller. You'll get a line containing an array `codes [NotBlank... ]`. Here you see what can be used as a key. (I'm usually good with `NotBlank.username = ...`) – MyBrainHurts Feb 02 '16 at 17:22
  • @codecannibal. you can configure your property file in context.xml file. – Nandu cg Feb 02 '16 at 17:44
  • @mybrainhurts . field error is not a form validation method – Nandu cg Feb 02 '16 at 17:46
  • With "System.out.println("\n" + result.getFieldErrors() + "\n");" i found what resource key to use - that helped me a lot. Thank you ! – CodeCannibal Feb 02 '16 at 17:50