Newsletter |
Spring MVC Annotation (JSR-303) Validation Tutorial
Spring MVC providing 3 types of validations hope you remember 🙂 if not have a look into the previous article. Annotation validation is one of them, let us see how to achieve annotation (JSR-303) validations in Spring MVC 3.
Friends, once again i am saying if you don’t know the basic concepts, you will not be able to understand this concept, so please go through these articles, [ignore if you already read]
Spring MVC Execution Flow Diagram, Spring MVC 3.2 Flow
Spring MVC Hello World, Spring MVC 3.2 Hello World Example In Eclipse
- Spring annotation validations are also known as JSR-303 validations
- We need to import javax.validation.constraints.* and org.hibernate.validator.constraints.* [depends upon your requirement, i am using both 🙂 will explain you in the example]
- For that we must have JSR-303 related jar, Hibernate Validator jar [ Don’t confuse..!, this is only for validations, nothing related to hibernate ] in you class path
You can download the same from the Maven repository…
Download validation-api-1.0.0.[JSR-303]
Download hibernate-validator-4.2.0.Final
Syntax
Consider some bean class…
Java4sBean.java
import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.Size; public class Java4sBean{ @NotEmpty private String user; @NotEmpty(message = "Password should not be blank.") @Size(min = 5, max = 8, message = "Password length should be between 5 to 8 Characters.") private String pass; // Setters and Getters... }
Then Controller class…
Java4sController.java
@Controller public class Java4sController { @RequestMapping("/someMappingName") public String loginCheck(@Valid Java4sBean bean, BindingResult result, ModelMap model) { if (result.hasErrors()) { return "loginPage"; } else { model.addAttribute("lfobj", bean); return "success"; } } }
In Spring Configuration File
In order to support direct bean validation we should do the following changes in your spring configuration file..
After
<context:component-scan base-package="java4s" />
you need to add the following line…
<mvc:annotation-driven />
so that JSR-303 validations will be activated 🙂
Explanation
I have to explain the controller class here, see Java4sController.java line number 5, i have written @Valid Java4sBean bean right, means once the flow came to this line and as we are using @Valid annotation, flow will redirect to Java4sBean and annotation validations will be executed. If get any errors all those errors will be added to the BindingResult object automatically. We can consider this BindingResult object as response in java servlets. That’s it friends, Just remember this concept i will explain the flow again in the example 🙂 cheers..!!!!!!!
You Might Also Like
::. About the Author .:: | ||
In the previous SpringMvcHelloWorld Example we used
public ModelAndView helloWorld()
and this example we used
public String loginCheck(@Valid Java4sBean bean, BindingResult result, ModelMap model)
for @RequestMapping
Please can you explain when to use what?
Thanks in advance
In How many ways we can apply validations in spring MVC ?
when I click the jsp page, i want to get values from db and displayed in dropdown please explain me.
You can use JSTL SQL functions as well as core functions for that…
how i will be give password validation like password will be content upper and lower content and must be content like % # @ & *
please tell me code
Excellent tutorial I don’t have words to tell about this site 🙂
Way of explanation is superb!!!!
It is very nice tutorial for novice
plz tell me iam getting error in helloworld example why iam getting that
Thank u very much for u r good explanation for each and every point.U r like u r hardworking nature keep it up boss.I never seen this type explanation for topic wise with especially for easy understandable english language.Once again thank you bhayya…………murtuza(Java developer)
Thank u so much sir u r explanation is vary nice i got more knowledge from u really thank u so much
Very helpful for the beginners
thank you for good understanding. You are explaining with picture. It is very useful to me and for better understanding.. keep it up…
nice exaplanation..easy to understand…
It is very nice tutorial for beginners
sir could u plz tell me what is the purpose of each parameter in validation example
Hi friend
It is very wonderful material for new spring learners.
but I need springMVC real time senarios and concepts
Thank you.
I need webservice communication with database or hibernate… thanks
Thanks you. Can you please provide login jsp page. here i want to know how to map the bean to component?
please explain
if (result.hasErrors()) {
return "loginPage";
} else {
model.addAttribute("lfobj", bean);
return "success";
}
}
}
please somebody provide spring configuration file and may be possible provide also login page