一、导包
链接:https://pan.baidu.com/s/1qc_95xhWRV4lgQOdh0WLNw
提取码:lclc
二、配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 数据验证 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator "></property>
</bean>
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!-- 在本配置文件中不要扫描service层和dao层 -->
<context:component-scan base-package="com.java.demo1.controller,com.java.exception"></context:component-scan>
<!-- 扫描springmvc中的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器(只对转发有效,重定向无效) -->
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
三、注解
在接收数据的实体类属性前添加校验的注解
package com.java.demo1.domain;
import java.util.Date;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
public class User {
//w代表数字、下划线和大小写字母,6~18
@Pattern(regexp="\\w{6,18}",message="用户名不合法") //@Pattern只能够修饰String类型
private String username;
//d代表只能是数字,6~18
@Pattern(regexp="\\d{6,18}",message="密码不合法")
private String password;
@Email(message="邮箱不合法")
private String email;
private String hobby;
private Date birthday;//支持这样的格式:1988/09/09;一般可以采用String来进行接收
@Min(value = 60,message="分数没有及格!!!")
@Max(value = 100,message="分数满分!!!")
private double score;//基础数据类型可以自动转换
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;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
四、测试
1、Jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="register.action" method="post">
用户名:
<input type="text" name="username"><font color="red">${errorMap.username }</font>
密码:
<input type="password" name="password"><font color="red">${errorMap.password }</font>
邮箱:
<input type="text" name="email"><font color="red">${errorMap.email }</font>
爱好:
<input type="checkbox" name="hobby" value="吃饭">吃饭
<input type="checkbox" name="hobby" value="睡觉">睡觉
<input type="checkbox" name="hobby" value="打豆豆">打豆豆
出生年月:
<input type="text" name="birthday">
分数:
<input type="text" name="score"><font color="red">${errorMap.score }</font>
<input type="submit" value="提交">
</form>
</body>
</html>
2、servlet
package com.java.demo1.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import com.java.demo1.domain.User;
@Controller //普通类被用Controller注释就变成了servlet
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo3 {
Map<String,String> errorMap = new HashMap<String,String>();
//映射方法
@RequestMapping("register.action")
public String register(@Valid User user,BindingResult rs,Model model) {
boolean hasErrors = rs.hasErrors();
if(hasErrors) {
//将字段对应的错误信息打印出来
List<FieldError> list = rs.getFieldErrors();
for (FieldError fieldError : list) {
//1、获取实体类中的属性名
String field = fieldError.getField();
//2、当数据不满足匹配规则时,获取错误提示信息
String error = fieldError.getDefaultMessage();
System.out.println(field+"-------------"+error);
errorMap.put(field, error);
}
model.addAttribute("errorMap", errorMap);
return "../register";
}else {
return "success";
}
}
}
在jsp页面中全部输入数据
其它验证