步骤一:导入四个jar包
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 配置包扫描器-->
<context:component-scan base-package="cn.happy.controller"></context:component-scan> <!-- 配置验证器 -->
<bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
</bean> <mvc:annotation-driven validator="myvalidator"/>
</beans>
步骤二:配置验证器
package cn.happy.entity; import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty;
import org.jboss.logging.Message; import com.sun.istack.internal.NotNull; public class UserInfo { //必须是0到100之间
@Min(value=0,message="成绩最小值为{value}")
@Max(value=100,message="成绩最大值为{value}")
private Integer score; //手机号码必须不能为空,必须是以1 开头 第二位3,4,5,6,7,8,9 最后9位随意
@NotEmpty(message="手机号码不允许为空")
@Pattern(regexp="^1[3,4,5,6,7,8,9]\\d{9}$",message="手机号码不正确")
private String phone; //不能为空
//必须是6个字符以上
@NotEmpty(message="用户名不能为空")
@Size(min=6,message="名称至少6个字符")
private String name; public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
步骤三:为实体类进行注解约束
package cn.happy.controller; import javax.validation.Valid; import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import cn.happy.entity.UserInfo; @Controller
public class FirstController {
@RequestMapping("/first.do")
public ModelAndView doFirst(@Valid UserInfo info,BindingResult br){
ModelAndView mv=new ModelAndView();
mv.setViewName("/WELCOME.jsp");
//记录到底是哪个字段验证失败了
//有一个可以侦测到验证错误总数的方法
int errorCount = br.getErrorCount();
if (errorCount>0) {
//证明模型验证失败
FieldError score = br.getFieldError("score");
FieldError name = br.getFieldError("name");
FieldError phone = br.getFieldError("phone");
if (score!=null) {
mv.addObject("scoremsg",score.getDefaultMessage());
} if (name!=null) {
mv.addObject("namemsg",name.getDefaultMessage());
} if (phone!=null) {
mv.addObject("phonemsg",phone.getDefaultMessage());
}
mv.setViewName("/index.jsp");
} //高中 英文版的吻别
return mv ;
}
}
步骤四:编写Handler控制器
<?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>
web.xml