springmvc之JSR303数据校验

1.搭建环境


    
      junit
      junit
      4.11
      test
    
    
	    commons-logging
	    commons-logging
	    1.1.3
	
	
	    org.springframework
	    spring-core
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-context
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-beans
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-expression
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-webmvc
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-web
	    4.0.5.RELEASE
	
	
	    org.springframework
	    spring-tx
	    4.0.5.RELEASE
	
        
	    commons-logging
	    commons-logging
	    1.1.1
	    
	
	    org.hibernate
	    hibernate-validator
	    4.3.0.Final
	
	
            org.jboss.logging
	    jboss-logging
            3.1.0.CR2
	
	
	    javax.validation
	    validation-api
	    1.0.0.GA
	
  

2.web.xml文件


		字符集过滤器
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			字符集编码
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
		1
	

	
		springmvc
		/
	

3.模型类

public class User {
	private Integer id;
	@NotEmpty
	private String username;
	@NotEmpty
	private String password;
	@Past
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birth;
	@NumberFormat(pattern="#,###,###.##")
	private double salary;
	@Email
	private String email;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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 Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", birth=" + birth + ", salary=" + salary
				+ ", email=" + email + "]";
	}
}

4.控制器

@RequestMapping("/viewResolver")
@Controller
public class ViewResolver {
	
	@RequestMapping("/testView")
	public String testView(@Validated User user,BindingResult result){
		System.out.println(user);
		if(result.getErrorCount() > 0){
			System.out.println("出错了!");
			for(FieldError error:result.getFieldErrors()){
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}
		}
		return "result";
	}
}

5.页面

input.jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Input


	
姓名:

密码:

生日:

薪水:

email:

结果:

springmvc之JSR303数据校验_第1张图片

springmvc之JSR303数据校验_第2张图片

JSR303就是一个为javabean的字段做合法性校验的框架。

常用注解:

注解 说明
@Null 注释的元素必须为空
@NotNull 注释的元素必须不为空
@AssertTrue 注释的元素必须为true
@AssertFalse 注释的元素必须为false
@Min(value) 注释的值为数字,且其大小必须大于最小值
@Max(value) 注释的值为数字,且其大小必须小于最大值
@DecimalMin(value) 注释的值为数字,且其大小必须大于等于最小值
@DecimalMax(value) 注释的值为数字,且其大小必须小于等于最大值
@Size(max,min) 注释的值的元素的大小必须在指定的范围内
@Digits(integer,fraction) 注释的值必须为数字,且其值须在范围内
@Past 注释的值元素为过去的日期
@Future 注释的值元素必须为将来的日期
@Pattern(value) 注释的值必须符合指定的正则表达式







你可能感兴趣的:(_____3.3,springmvc)