采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证。首先我们要到http://hibernate.org/validator/下载需要的jar包,这里以4.3.1.Final作为演示,解压后把hibernate-validator-4.3.1.Final.jar、jboss-logging-3.1.0.jar、validation-api-1.0.0.GA.jar这三个包添加到项目中。
/wzz/src/main/resources/applicationContext-mvc.xml
<?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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 添加注解驱动 --> <mvc:annotation-driven /> <context:component-scan base-package="com.controller" /> <!-- 对某些静态资源,如css,js,图片等进行过滤 ,有引用 "/resources/**" 的路径引用转到工程的/resources/目录取资源 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- jsp 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven validator="validator" conversion-service="conversion-service" /> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!--不设置则默认为classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="validatemessageSource"/> </bean> <bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:validatemessages"/> <property name="fileEncodings" value="utf-8"/> <property name="cacheSeconds" value="120"/> </bean> </beans>
/wzz/src/main/java/com/controller/GeneralController.java
package com.controller; 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 com.java.LoginMessage; @Controller public class GeneralController { @RequestMapping(value="/login_input", method = {RequestMethod.GET}) public String login_input(Model model){ if(!model.containsAttribute("loginMessage")){ model.addAttribute("loginMessage", new LoginMessage()); } return "login"; } @RequestMapping(value="/login_input", method = {RequestMethod.POST}) public String login_save(@Valid @ModelAttribute("loginMessage") LoginMessage loginMessage, BindingResult bindingresult, Model model){ if(bindingresult.hasErrors()){ return login_input(model); } return "success"; } }
/wzz/src/main/java/com/java/LoginMessage.java
package com.java; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; public class LoginMessage { @Pattern(regexp="^[a-zA-z0-9]+$", message="not has space.") private String fullname; @Size(min=3, max=20, message="The password must be at least 6 charaters long.") private String password; public String getFullname() { return fullname; } public void setFullname(String fullname) { this.fullname = fullname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
/wzz/src/main/webapp/WEB-INF/views/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form:form modelAttribute="loginMessage" method="post"> <!-- path="*" 错误提示一起出现 --> <%-- <form:errors path="*"></form:errors><br/><br/> --%> fullname:<form:input path="fullname" /><br/> <form:errors path="fullname"></form:errors><br/> password:<form:input path="password" /><br/> <form:errors path="password"></form:errors><br/> <input type="submit" value="Submit" /> </form:form> </body> </html>
原文:http://www.cnblogs.com/wzz1020/p/4841577.html