I’ve a jsp page which is like this:
<form:form action="userRegistration" name="userRegistrationForm" commandName="userRegistration">
<form:input path="userName"/>
<form:input path="houseNo"/>
</form:form>
and all the required fields.
In controller I use like this:
@RequestMapping(method=RequestMethod.GET)
public String registration(ModelMap model,HttpSession session) {
UserRegistration userRegistration = service.createUserRegistration();
model.addAttribute("userRegistration",userRegistration);
return "registrationview";
}
@RequestMapping(method=RequestMethod.POST)
public String getRegistration(UserRegistration userRegistration, HttpSession session, ModelMap model) {
boolean result = validateAndSaveData(userRegistration);
if (result){
}
}
when the user submits the form, it comes to the post and then validates the input and saves it.
How can I send a succes message and error message to the view. Is there any changes required in the existing implementation ?
I agree with Shagaan that you should look into using Spring’s built-in support for JSR-303 validation to validate the form. Here’s a little info on how to do that. First, annotate your form with JSR-303 (javax.validation) annotations:
Then annotate your model in the controller method with “@Valid”:
Spring’s BindingResult automatically binds the errors to your form object, so then in your jsp, you could do:
To use Spring validation, you need Validation-api (JSR-303 spec) and an implementation (Hibernate-Validator) in your classpath. See Section 6.7 of Spring validation docs