Springmvc数据校验
步骤一:导入四个jar包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 14 "> 15 16 <!-- 配置包扫描器--> 17 <context:component-scan base-package="cn.happy.controller"></context:component-scan> 18 19 <!-- 配置验证器 --> 20 <bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 21 <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> 22 </bean> 23 24 <mvc:annotation-driven validator="myvalidator"/> 25 </beans>
1 package cn.happy.entity; 2 3 import javax.validation.constraints.Max; 4 import javax.validation.constraints.Min; 5 import javax.validation.constraints.Pattern; 6 import javax.validation.constraints.Size; 7 8 import org.hibernate.validator.constraints.NotEmpty; 9 import org.jboss.logging.Message; 10 11 import com.sun.istack.internal.NotNull; 12 13 public class UserInfo { 14 15 //必须是0到100之间 16 @Min(value=0,message="成绩最小值为{value}") 17 @Max(value=100,message="成绩最大值为{value}") 18 private Integer score; 19 20 //手机号码必须不能为空,必须是以1 开头 第二位3,4,5,6,7,8,9 最后9位随意 21 @NotEmpty(message="手机号码不允许为空") 22 @Pattern(regexp="^1[3,4,5,6,7,8,9]\\d{9}$",message="手机号码不正确") 23 private String phone; 24 25 //不能为空 26 //必须是6个字符以上 27 @NotEmpty(message="用户名不能为空") 28 @Size(min=6,message="名称至少6个字符") 29 private String name; 30 31 public Integer getScore() { 32 return score; 33 } 34 public void setScore(Integer score) { 35 this.score = score; 36 } 37 public String getPhone() { 38 return phone; 39 } 40 public void setPhone(String phone) { 41 this.phone = phone; 42 } 43 public String getName() { 44 return name; 45 } 46 public void setName(String name) { 47 this.name = name; 48 } 49 }
1 package cn.happy.controller; 2 3 import javax.validation.Valid; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.validation.BindingResult; 7 import org.springframework.validation.FieldError; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.servlet.ModelAndView; 10 11 import cn.happy.entity.UserInfo; 12 13 @Controller 14 public class FirstController { 15 @RequestMapping("/first.do") 16 public ModelAndView doFirst(@Valid UserInfo info,BindingResult br){ 17 ModelAndView mv=new ModelAndView(); 18 mv.setViewName("/WELCOME.jsp"); 19 //记录到底是哪个字段验证失败了 20 //有一个可以侦测到验证错误总数的方法 21 int errorCount = br.getErrorCount(); 22 if (errorCount>0) { 23 //证明模型验证失败 24 FieldError score = br.getFieldError("score"); 25 FieldError name = br.getFieldError("name"); 26 FieldError phone = br.getFieldError("phone"); 27 if (score!=null) { 28 mv.addObject("scoremsg",score.getDefaultMessage()); 29 } 30 31 if (name!=null) { 32 mv.addObject("namemsg",name.getDefaultMessage()); 33 } 34 35 if (phone!=null) { 36 mv.addObject("phonemsg",phone.getDefaultMessage()); 37 } 38 mv.setViewName("/index.jsp"); 39 } 40 41 //高中 英文版的吻别 42 return mv ; 43 } 44 }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- 编码过滤器 --> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置中央调度器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!-- TOmcat启动的时候,Servlet对象就存储到内存 正整数 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>