This example presents the basic concept of using in Spring MVC bean validation.
The technologies used are :
-
– Spring 4.3.18
– JDK 1.80
– Maven 3.3.9
You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :
mvn eclipse:eclipse
Inspired from « Beginning Spring » Mert Caliskan, Kenan Sevindik, Rod Johnson (Foreword by), Jürgen Höller (Foreword by).
package javaspringexamples.spring.beanvalidation.domain; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; /** * * @author mounir.sahrani@gmail.com * */ public class User { @Size(min = 3, max = 20) String username; @Pattern(regexp = "^[a-zA-Z]\\w{3,14}$") String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Pattern.user.password=Error format: at least 4 letters, max 15. Size.user.username=Error format: at least 3, max 20.
package javaspringexamples.spring.beanvalidation.controller; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import javaspringexamples.spring.beanvalidation.domain.User; import javax.validation.Valid; /** * * @author mounir.sahrani@gmail.com * */ @Controller public class UserController { @RequestMapping(value = "/form") public ModelAndView initiateUser() { return new ModelAndView("userForm", "user", new User()); } @RequestMapping(value = "/result", method = RequestMethod.POST) public ModelAndView processUser(@Valid User user, BindingResult result) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("usr", user); if (result.hasErrors()) { modelAndView.setViewName("userForm"); } else { modelAndView.setViewName("userResult"); } return modelAndView; } }
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.mvc</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="javaspringexamples.spring.beanvalidation.controller" /> <context:annotation-config /> <mvc:annotation-driven validator="validator" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:errorMessages" /> </bean> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="validationMessageSource" ref="messageSource" /> </bean> </beans>
<% response.sendRedirect("form.mvc"); %>
<%@ page contentType="text/html; charset=ISO-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"%> <html> <head> <title>JavaSpringExamples Spring MVC Form</title> <style type="text/css"> .formFieldError { background-color: #FFC; } </style> </head> <body> <h2>Form</h2> <mvc:form modelAttribute="user" action="result.mvc"> <table> <tr> <td><mvc:label path="username">Login</mvc:label></td> <td><mvc:input path="username" cssErrorClass="formFieldError" /></td> <td><mvc:errors path="username" /></td> </tr> <tr> <td><mvc:label path="password">Password</mvc:label></td> <td><mvc:password path="password" cssErrorClass="formFieldError" /></td> <td><mvc:errors path="password" /></td> </tr> <tr> <td colspan="3"><input type="submit" value="Submit" /></td> </tr> </table> </mvc:form> </body> </html>
<%@ page contentType="text/html; charset=ISO-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"%> <html> <head> <title>JavaSpringExamples Spring MVC Form</title> </head> <body> <h2>Result Form</h2> <table> <tr> <td>login :</td> <td>${usr.username}</td> </tr> <tr> <td>Password :</td> <td>${usr.password}</td> </tr> </table> </body> </html>
Get the sources of the example from the following GitHub url