相对于Spring本身的validation框架,
JSR 303校验显得更简单,易操作。
关键的两个文件如下:
User.java
package org.fkit.domain;
import java.io.Serializable;
import java.util.Date;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.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(message="登陆名不能为空")
private String loginName;
@NotBlank(message="密码不能为空")
@Length(min=6, max=8, message="密码长度必须在6位到8位之间")
private String password;
@NotBlank(message="用户名不能为空")
private String username;
@Range(min=15, max=60, message="年龄必须在15岁到60岁之间")
private int age;
@Email(message="必须是合法的邮箱地址")
private String email;
@DateTimeFormat(pattern="yyyy-MM-dd")
@Past(message="生日必须是一个过去的日期")
private Date birthDate;
@Pattern(regexp="[1][3,8][3,6,9][0-9]{8}", message="无效的电话号码")
private String phone;
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 String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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 Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
registerForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>测试JSR 303</title>
</head>
<body>
<h3>注册页面</h3>
<br>
<form:form action="login" method="post" modelAttribute="user">
<table>
<tr>
<td><label>登陆名: </label></td>
<td><form:input path="loginName" /></td>
<td><form:errors path="loginName" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>密码: </label></td>
<td><form:input path="password" /></td>
<td><form:errors path="password" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>用户名: </label></td>
<td><form:input path="username" /></td>
<td><form:errors path="username" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>年龄: </label></td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>邮箱: </label></td>
<td><form:input path="email" /></td>
<td><form:errors path="email" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>生日: </label></td>
<td><form:input path="birthDate" /></td>
<td><form:errors path="birthDate" cssStyle="color:red" /></td>
</tr>
<tr>
<td><label>电话: </label></td>
<td><form:input path="phone" /></td>
<td><form:errors path="phone" cssStyle="color:red" /></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
![32855d670415187a4af67bed7333d746201ffcbb](https://i-blog.csdnimg.cn/blog_migrate/b0a14311ca731f7111dc85787e7df489.png)