Spring MVC Annotation (JSR-303) Validation Tutorial

Spring-MVC » on Jul 24, 2013 { 20 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…

import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Size;
public class Java4sBean{

    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…

public class Java4sController {    

        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..

<context:component-scan base-package="java4s" />

you need to add the following line…

<mvc:annotation-driven />

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 the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


20 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. 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. 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?

  18. Arif says:

    please explain

    if (result.hasErrors()) {
    return "loginPage";
    } else {
    model.addAttribute("lfobj", bean);
    return "success";

  19. rJat says:

    please somebody provide spring configuration file and may be possible provide also login page


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

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