Spring MVC Tutorial

Spring MVC @valid annotation Example

Spring MVC @valid annotation:

This tutorial shows you how to validate a bean with jsr 303 standard and uses the properties file to load the error message (resource bundle).

JSR 303 bean validation using javax standards

package com.candidjava.spring.bean;

import java.util.List;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class User 
{	
  @NotNull
  @Size(min=1)
  private String name;
  
  @NotNull
  @Size(min=1)
  private List<String> language;
  
  @Size(min=1)
  private String country;
  
  @NotNull
  private String gender;
  
  @Size(min=8, max=20)
  private String password;
  
  @Size(min=1, message="Email feild can't blank")
  private String email;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<String> getLanguage() {
    return language;
  }

  public void setLanguage(List<String> language) {
    this.language = language;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
}

@valid in spring controller

package com.candidjava.spring.controller;


import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.candidjava.spring.bean.User;

@Controller
public class UserController {
  
  User users = new User();
  @GetMapping("index")
  public ModelAndView register(User user) {
    return new ModelAndView("register");
  }

  @PostMapping("/register")
  public ModelAndView create(@Valid User user, BindingResult bindingResult) {
    ModelAndView model = new ModelAndView("view");
    // user bean will be automatically binded to view refer @ModelAttribute
    if(bindingResult.hasErrors())
    {
      return new ModelAndView("register");
    }
      users.setName(user.getName());
      users.setEmail(user.getEmail());
      users.setGender(user.getGender());
      users.setLanguage(user.getLanguage());
      users.setCountry(user.getCountry());
      users.setPassword(user.getPassword());
    return model;
  }

  @GetMapping("/register")
  public ModelAndView viewData(User user) {

    ModelAndView model = new ModelAndView("register");
    return model;
  }

}

Resource file for spring validation message

Size.user.name = please enter name
NotNull.user.gender = Please specify your gender
Size.user.country = Please select your country
Size.user.language= Please select at least one language
Size.user.password = password must be between 8 and 20
typeMismatch=Invalid format

Jsp view

<%@page import="org.springframework.validation.ObjectError"%>
<%@ page import="com.candidjava.spring.bean.User" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.err
{
color:red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>

  <h2>Simple spring form handling</h2>
  <form:form action="register" method="POST" modelAttribute="user">
  Name:<form:input path="name"/><p><form:errors path="name" class="err"/></p><br>
Gender : Male<form:radiobutton name="gender" value="male" path="gender"/>   Female<form:radiobutton  name="gender" value="female" path="gender"/><p><form:errors path="gender"  class="err"/></p><br>
  Email : <form:input  path="email"/><p><form:errors path="email"  class="err"/></p><br>
  Languages : English<form:checkbox   value="english" path="language" /> French<form:checkbox  value="french" path="language"/> Tamil<form:checkbox  value="tamil" path="language"/><p><form:errors path="language"  class="err"/></p><br>
  Country : <form:select path="country">
          <form:option value="">select country</form:option>
          <form:option value="india">INDIA</form:option>
          <form:option value="usa">USA</form:option>
          <form:option value="china">CHINA</form:option>
         </form:select>
         <p><form:errors path="country" class="err"/></p>
         
        <br>  
        Password:<form:password path="password"/><p><form:errors path="password" class="err"/></p><br>  
        <input type="submit" value="submit" />
  </form:form>
</body>
</html>

 

Download

Spring 4 form validation example war

Spring 4 form validation example maven zip