springmvc之JSR303数据校验

1.搭建环境

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.1.3</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-beans</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-expression</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-tx</artifactId>
	    <version>4.0.5.RELEASE</version>
	</dependency>
        <dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.1.1</version>
	    </dependency>
	<dependency>
	    <groupId>org.hibernate</groupId>
	    <artifactId>hibernate-validator</artifactId>
	    <version>4.3.0.Final</version>
	</dependency>
	<dependency>
            <groupId>org.jboss.logging</groupId>
	    <artifactId>jboss-logging</artifactId>
            <version>3.1.0.CR2</version>
	</dependency>
	<dependency>
	    <groupId>javax.validation</groupId>
	    <artifactId>validation-api</artifactId>
	    <version>1.0.0.GA</version>
	</dependency>
  </dependencies>

2.web.xml文件

<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集编码</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:conf/spring.xml</param-value>
		</init-param> -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

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"%>
<!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>Input</title>
</head>
<body>
	<form action="viewResolver/testView" method="post">
		姓名:<input type="text" name="username"/><br><br>
		密码:<input type="password" name="password"/><br><br>
		生日:<input type="text" name="birth"/><br><br>
		薪水:<input type="text" name="salary"/><br><br>
		email:<input type="text" name="email"/><br><br>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>
结果:



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)注释的值必须符合指定的正则表达式







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值