1.JSR 303表单验证
这种方法比较适合银行类型使用的一些项目,涉及的安全度比较高时使用。所以,只要能看懂,会用就可以了。一般的项目中用不到
2.详细信息

3.需要的jar包:

4.实例
Validate实体类: 主要看注解
package com.gb.pojo;
import java.util.Date;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.springframework.format.annotation.DateTimeFormat;
public class Validate {
@NotNull
private Integer id;
@NotNull
@DecimalMin("1.0")// 最小值
@DecimalMax("300000.0")// 最大值
private Double price;
@NotNull
@Past
@DateTimeFormat(pattern="yyyy-MM-dd")//可以解析的日期格式
private Date date;
@NotNull
@Min(1)
@Max(300)
private Integer value;
@Size(min=10,max=200)
private String note;
@Pattern(regexp = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$", message="邮箱格式不正确")
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}}
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="val/validate.do" method="post">
ID:<input type="text" name="id"/><br/>
价格:<input type="text" name="price"/><br/>
日期:<input type="text" name="date"/><br/>
值 :<input type="text" name="value"/><br/>
备注:<input type="text" name="note"/><br/>
邮箱:<input type="text" name="email"/><br/>
<input type="submit" value="提交"/><br/>
</form>
</body>
</html>
ValidateController.java
package com.gb.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gb.pojo.Validate;
@Controller
@RequestMapping("val")
public class ValidateController {
@RequestMapping("validate")
public String validate(@Valid Validate validate, Errors errors) {
List<FieldError> fieldErrors = errors.getFieldErrors();
// 得到验证的错误信息,之后可以加入自己的处理逻辑,此处只是将它们输出
for(FieldError fe:fieldErrors) {
System.out.println(fe.getField() + ", " + fe.getDefaultMessage());
}
return "test";
}}
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<%=session.getAttribute("id") %><br/>
<%=session.getAttribute("name") %><br/>
<%=session.getAttribute("note") %><br/>
<%=session.getAttribute("role") %><br/>
</body>
</html>
网友评论