spring mvc 采用 jsr303 bean validation 校验框架

这是一个规范,定义了一些元素来进行bean的数据校验,比如 你的model有一个 user.java ,里面有一个email,当用户注册时候要验证email是否合法。 一般做法是js前端校验,但是不安全,作为完整安全解决方案,我们必须后端校验。

 

 

表 1. Bean Validation 中内置的 constraint

 

 

Constraint 详细信息

@Null 被注释的元素必须为 null

@NotNull 被注释的元素必须不为 null

@AssertTrue 被注释的元素必须为 true

@AssertFalse 被注释的元素必须为 false

@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值

@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值

@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

@Size(max, min) 被注释的元素的大小必须在指定的范围内

@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内

@Past 被注释的元素必须是一个过去的日期

@Future 被注释的元素必须是一个将来的日期

@Pattern(value) 被注释的元素必须符合指定的正则表达式hibernate对这个规范做了实现和扩展;

 

Constraint 详细信息

@Email 被注释的元素必须是电子邮箱地址

@Length 被注释的字符串的大小必须在指定的范围内

@NotEmpty 被注释的字符串的必须非空

@Range 被注释的元素必须在合适的范围内

2,jsr303实现

目前实现较好的 validation有 Hibernate Validator,它 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。 可以独立使用,哪怕你的架构是 spring mvc + ibatis 也可以单独使用此校验框架。 好处看下面就知道了:

 

 

 

3,spring mvc中如何使用jsr303 validation

 

1)首先要基于annotion注解驱动,配置spring中配置

   

<mvc:annotation-driven />  

 

 

2)相关jar包,如果用的是maven

 

 

 

 

[html]  

<repositories>  

        <repository>  

            <id>JBoss repository</id>  

            <url>http://repository.jboss.org/nexus/content/groups/public/</url>  

        </repository>  

    </repositories>  

   

    <properties>  

        <spring.version>3.0.5.RELEASE</spring.version>  

    </properties>  

   

    <dependencies>  

   

        <!-- Spring 3 dependencies -->  

        <dependency>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-core</artifactId>  

            <version>${spring.version}</version>  

        </dependency>  

   

        <dependency>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-web</artifactId>  

            <version>${spring.version}</version>  

        </dependency>  

   

        <dependency>  

            <groupId>org.springframework</groupId>  

            <artifactId>spring-webmvc</artifactId>  

            <version>${spring.version}</version>  

        </dependency>  

   

        <!-- Hibernate Validator -->  

        <dependency>  

            <groupId>org.hibernate</groupId>  

            <artifactId>hibernate-validator</artifactId>  

            <version>4.2.0.Final</version>  

        </dependency>  

   

    </dependencies>  

 

 

如果不是maven,单独下来hibernate-validator.jar到工程即可。

 

3)在model(数据交互的一些对象,或者vo)中声明

           

 

[java]  

@NotEmpty(message="名字不能为空")  

    private String name;  

  

  @NotEmpty(message="email不能为空")  

  @Email(message="email格式不合法")  

    private String email;  

 

 

[java]  

4)在controller中使用@valid 和BindingResult :  

[java] view plaincopy

<span style="font-size:18px;">@RequestMapping(value="/test")  

@ResponseBody  

private User test(@Valid User u,BindingResultresult){  

if(result.hasErrors()){//这个是spring validation校验后返回的结果对象  

//如果校验失败,你要做什么事情  

String code = result.getFieldError().getCode();//验证出错的那个类型名称 比如NotEmpty  

        result.getFieldError().getDefaultMessage());//出错的信息  

return m;  

}else{  

return null;  

}  

  

}</span>  

 

 

    5)页面上如果需要显示错误,要么通过el表达式取model中值,也可以使用spring form标签:

 

 

<form:errors path="*" cssClass="errorblock" element="div" />

  最后,我使用的是spring mvc+ mybatis, hibernate validation是可独立的一块和orm并无太大关系。 ibatis在validation规范并没有实现。

 


转载于:https://my.oschina.net/moziqi/blog/339235

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值