Get all updates of Java4s
Follow Us: Email Newsletter

Spring MVC Annotation (JSR-303) Validation Tutorial

Spring-MVC » On Jul 24, 2013 { 17 Comments } By Sivateja

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


Consider some bean class…

Then Controller class…

In Spring Configuration File

In order to support direct bean validation we should do the following changes in your spring configuration file..

you need to add the following line…

so that JSR-303 validations will be activated :-)


I have to explain the controller class here, see 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


17 Responses to “Spring MVC Annotation (JSR-303) Validation Tutorial”
  1. Divya says:

    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

  2. MaheshNasana says:

    In How many ways we can apply validations in spring MVC ?

  3. maheswari says:

    when I click the jsp page, i want to get values from db and displayed in dropdown please explain me.

  4. shubhrajyoti says:

    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

  5. sharmila says:

    Excellent tutorial I don’t have words to tell about this site :-)

    Way of explanation is superb!!!!

  6. pawan kumar says:

    It is very nice tutorial for novice

  7. farha says:

    plz tell me iam getting error in helloworld example why iam getting that

  8. Murthuja vali says:

    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)

  9. satish says:

    Thank u so much sir u r explanation is vary nice i got more knowledge from u really thank u so much

  10. Vinay says:

    Very helpful for the beginners

  11. Jeeva says:

    thank you for good understanding. You are explaining with picture. It is very useful to me and for better understanding.. keep it up…

  12. prabha says:

    nice exaplanation..easy to understand…

  13. Teja says:

    It is very nice tutorial for beginners

  14. obireddy says:

    sir could u plz tell me what is the purpose of each parameter in validation example

  15. lingaiah says:

    Hi friend

    It is very wonderful material for new spring learners.

    but I need springMVC real time senarios and concepts

    Thank you.

  16. Pramod says:

    I need webservice communication with database or hibernate… thanks

  17. kannan says:

    Thanks you. Can you please provide login jsp page. here i want to know how to map the bean to component?


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.