<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.2.Final</version>
</dependency>
jsr303注解:https://www.cnblogs.com/rocky-AGE-24/p/5245022.html
jsp表单:
<form action="<%=path%>/reg" method="post">
用户名:<input type="text" name="userName" /><font color="red"><form:errors path="user.userName"></form:errors></font></br>
年龄:<input type="text" name="age" /><font color="red"><form:errors path="user.age"></form:errors></font></br>
密码:<input type="password" name="password" /><font color="red"><form:errors path="user.password"></form:errors></font></br>
确认密码:<input type="password" name="repassword"><font color="red"><form:errors path="user.repassword"></form:errors></font></br>
手机号码:<input type="text" name="phone" /><font color="red"><form:errors path="user.phone"></form:errors></font></br>
邮箱:<input type="text" name="email" /><font color="red"><form:errors path="user.email"></form:errors></font></br>
个人网址:<input type="text" name="website"><font color="red"><form:errors path="user.website"></form:errors></font></br>
出生日期: <input type="date" name="date" /> <br>
<input type="button" οnclick="checkSubmit()" value="提交">
</form>
打印出表单验证不通过的信息:
1.导入标签:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:errors path="bean名.属性名"></form:errors></font>
2. web.xml配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3. 添加spring.xml文件
controller:
建一个bean,bean的属性名为表单中的name
public class UserInfo {
@NotEmpty(message = "用户名不能为空")
private String userName;
@NotEmpty(message = "年龄不能为空")
@Range(min = 1, max = 120, message = "输入的年龄不合法")
private String age;
@NotEmpty(message = "密码不能为空")
@Size(max = 11, message = "输入的密码不能超过11位")
private String password;
@NotEmpty(message = "再次输入不能为空")
private String repassword;
@Size(min = 11, max = 11, message = "手机号码必须是11位")
private String phone;
@Email(message = "输入的不是一个合法的邮箱地址")
private String email;
@Pattern(message = "输入的网址格式不对", regexp = "^((https|http)?:\\/\\/)[^\\s]+")
private String website;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
@Valid UserInfo user,BindingResult error
@Valid:将表单中提交的数据绑定到对象中进行验证
BindingResult或Errors类型:保存校验结果的入参,BindingResult是Errors的子类
@RequestMapping(value="/reg",method=RequestMethod.POST)
public String reg(@ModelAttribute("user") @Valid UserInfo user,BindingResult error) {
System.out.println(user.getDate());
if(!user.getPassword().equals(user.getRepassword())){
error.addError(new FieldError("user", "repassword", "两次密码不一致"));
}
if(error.hasErrors()){
return "/xx.jsp";
}
return "/xx.jsp";
}