一 资源文件
1 中文
NotBlank.user.loginname=\u767B\u5F55\u540D\u4E0D\u80FD\u4E3A\u7A7A
NotBlank.user.password=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
Length.user.password=\u5BC6\u7801\u957F\u5EA6\u5FC5\u987B\u57286\u4F4D\u52308\u4F4D\u4E4B\u95F4
NotBlank.user.username=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
Range.user.age=\u5E74\u9F84\u5FC5\u987B\u572815\u523060\u5C81\u4E4B\u95F4
Email.user.email=\u5FC5\u987B\u662F\u5408\u6CD5\u7684\u90AE\u7BB1\u5730\u5740
Past.user.birthday=\u751F\u65E5\u5FC5\u987B\u662F\u4E00\u4E2A\u8FC7\u53BB\u7684\u65E5\u671F
Pattern.user.phone=\u65E0\u6548\u7684\u7535\u8BDD\u53F7\u7801
2 英文
NotBlank.user.loginname= Loginname is not null
NotBlank.user.password= Password is not null
Length.user.password=Password length must be between 6 and 8
NotBlank.user.username= Username is not null
Range.user.age=Age must be between the ages of 15 to 60
Email.user.email=Must be a legitimate email address
Past.user.birthday=Birthday must be a date in the past
Pattern.user.phone=Invalid phone number
二 领域模型
package org.fkit.domain;
import java.io.Serializable;
import java.util.Date;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Email;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@NotBlank
private String loginname;
@NotBlank
@Length(min=6,max=8)
private String password;
@NotBlank
private String username;
@Range(min=15, max=60)
private int age;
@Email
private String email;
@DateTimeFormat(pattern="yyyy-MM-dd")
@Past
private Date birthday;
@Pattern(regexp="[1][3,8][3,6,9][0-9]{8}")
private String phone;
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [loginname=" + loginname + ", password=" + password
+ ", email=" + email + ", username="
+ username + ", birthDate=" + birthday + ", phone=" + phone
+ "]";
}
}
三 控制器
package org.fkit.controller;
import javax.validation.Valid;
import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController{
@GetMapping(value="/registerForm")
public String registerForm(Model model){
User user = new User();
model.addAttribute("user",user);
// 跳转到注册页面
return "registerForm";
}
// 数据校验使用@Valid,后面跟着Errors对象保存校验信息
@PostMapping(value="/login")
public String login(
@Valid @ModelAttribute User user,
Errors errors,
Model model) {
System.out.println(user);
if(errors.hasErrors()){
return "registerForm";
}
model.addAttribute("user", user);
return "success";
}
}
四 视图
1 registerForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试JSR 303</title>
</head>
<body>
<h3>注册页面</h3>
<form:form modelAttribute="user" method="post" action="login" >
<table>
<tr>
<td>登录名:</td>
<td><form:input path="loginname"/></td>
<td><form:errors path="loginname" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>密码:</td>
<td><form:input path="password"/></td>
<td><form:errors path="password" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>用户名:</td>
<td><form:input path="username"/></td>
<td><form:errors path="username" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><form:input path="age"/></td>
<td><form:errors path="age" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>邮箱:</td>
<td><form:input path="email"/></td>
<td><form:errors path="email" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>生日:</td>
<td><form:input path="birthday"/></td>
<td><form:errors path="birthday" cssStyle= "color:red"/></td>
</tr>
<tr>
<td>电话:</td>
<td><form:input path="phone"/></td>
<td><form:errors path="phone" cssStyle= "color:red"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form:form>
</body>
</html>
2 success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试JSR 303</title>
</head>
<body>
<h3>测试JSR 303</h3>
登录名:${requestScope.user.loginname }
密码:${requestScope.user.password }
用户名:${requestScope.user.username }
年龄:${requestScope.user.age }
邮箱:${requestScope.user.email }
生日:<fmt:formatDate value="${requestScope.user.birthday}"
pattern="yyyy年MM月dd日"/>
电话:${requestScope.user.phone }
</body>
</html>
五 配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit"/>
<!-- 默认装配方案 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 p:prefix属性表示前缀 p:suffix 表示后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化资源文件名 -->
<property name="basenames" value="message"/>
</bean>
</beans>
六 测试