Spring MVC validation Example

Spring MVC validation Example

In the last example , we developed an MVC application.But it does not have any validations.If some validation criteria needs to meet while submitting the form , spring has the provisions to do the same. JSR-303 is the specification for been validations in Java .We are using the hibernate validator as the implementation.We are just inserting a validation in to the previous login form example.

So the work space set up and files are almost same.Some files needs to be edited.

pom.xml

The pom.xml needs to include any of the JSR-303 validation implementation.We are using hibernate-validator


	4.0.0
	com.samples
	SpringSample
	war
	0.0.1-SNAPSHOT
	SpringSample Maven Webapp
	http://maven.apache.org
	
		4.1.0.RELEASE
	
	
		
			junit
			junit
			3.8.1
			test
		
		
			org.springframework
			spring-core
			${org.springframework-version}
		

		
			org.springframework
			spring-context
			${org.springframework-version}
		
		
			org.springframework
			spring-webmvc
			${org.springframework-version}
		
		
			org.springframework
			spring-beans
			${org.springframework-version}
		
		
			org.springframework
			spring-expression
			${org.springframework-version}
		
		
			javax.servlet
			jstl
			1.2
		
		
		
		
		

    javax.validation
    validation-api
    1.1.0.Final


    org.hibernate
    hibernate-validator
    5.0.1.Final

	
	
		SpringSample
	

loginform.jsp

It needs to include the form errors in the page so that if validation fails,it can be shown.We can give suitable css properties also , if required.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>



Please Login


	
Login Id
Password

LoginController.java

It needs to render the same login page if validation fails.Otherwise the success page needs to be rendered.

import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sample.models.User;

@Controller
@RequestMapping("/")
public class LoginController {
	
	
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public ModelAndView login(Model model) {
		System.out.println("Re directing to login page");
		model.addAttribute("user", new User()); 
		return new ModelAndView("loginpage");
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public ModelAndView doLogin(@ModelAttribute ("user")@Valid User user,
		
	        BindingResult result) {
		System.out.println("Received User  Details : "+user);
		String resultPage = "successpage";
		if(result.hasErrors()){
			resultPage = "loginpage";
		}
		return new ModelAndView(resultPage);
	}
}

User.java

The validation rules are mentioning in the command class itself.Since we are dealing with a very basic example , we are using the default validation messages which the frameworks provides by default.

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

public class User {

	@NotNull
	@Size(min = 3, max = 10)
	private String userId;

	@NotNull
	@Size(min = 10, max = 20)

	private String password;

	public User(String userId, String password) {
		
		this.userId = userId;
		this.password = password;
	}
	
	public User(){
		this.userId = "";
		this.password = "";
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getPassword() {
		return password;
	}

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

	public String toString() {
		return "User Id = " + userId + " ; Password = " + password;
	}

}

dispatcher-servlet.xml 




	
	
 
	

	
		
			/WEB-INF/pages/
		
		
			.jsp
		
	

mvc:annotation-driven is added for validations.

Output

Now lets see the output .Just run the application in server and test the page with various conditions. User id should be within 3 to 10 characters. And password should be within 10 to 20.If the entered data is not matching the requirements , then the same form with validation messages will be rendered.

validationoutput

    See Related:

Spring Overview
Spring MVC Example