1.spring mvc配置文件中添加:
        <mvc:annotation-driven />
2.pom.xml中添加

       <dependency > 
            <groupId> org.hibernate</groupId > 
            <artifactId> hibernate-validator</artifactId>  
            <version> 5.1.0.Final</version > 
        </dependency>
       <dependency > 
            <groupId> org.slf4j</groupId > 
            <artifactId> slf4j-simple</artifactId > 
            <version> 1.7.7</ version> 
        </dependency>
 
          若编译报错,尝试其他版本。
3.pojo中添加注解
     
     @NotEmpty(message= "用户名不能为空" )
    private String userName;
 
    @Size(min=1,max=3)
    private String password;
 
    @Max(value=120,message= "年龄不能超过120岁" )
    private Integer age;
 
4.controller中添加@Valid标记。

     @RequestMapping(value= "/user_save" )
public String saveUser( @Valid @ModelAttribute User user , BindingResult bindingResult ,
Model model) { if (bindingResult .hasErrors()){
List<ObjectError> errorList = bindingResult .getAllErrors();
System. out .println("errorList大小为:" +errorList .size());
for (ObjectError error : errorList ){
System.out .println(error .getDefaultMessage());
}
return "AddUser" ;
}
}

  这里一个@Valid的参数后必须紧挨着一个BindingResult 参数,否则spring会在校验不通过时直接抛出异常。

5.jsp中显示error信息
     < form:errors path ="password" cssClass ="error" />
 

    自定义校验类型 http://exceptioneye.iteye.com/blog/1305040

    两种校验方式使用说明 http://elf8848.iteye.com/blog/1299587

使用JSR验证的更多相关文章

  1. Spring + Spring MVC + MyBatis 整合

    1.所需要Jar包 ? <!-- Spring3.0.1包 -->   org.springframework.web-3.0.1 系列   <!-- 公共包 -->   sl ...

  2. Spring MVC4 + Spring Security4 + Hibernate实例

    http://www.yiibai.com/spring-security/spring-mvc-4-and-spring-security-4-integration-example.html 在这 ...

  3. JSR 303 - Bean Validation 模型验证

    类是转载的,不知道转的哪里的. 此类依赖 JSR 303 – Bean Validation, Hibernate Validator. 代码不能直接运行.意会一下.自己改改. import com. ...

  4. springmvc 整合数据验证框架 jsr

    1.maven <dependency> <groupId>javax.validation</groupId> <artifactId>validat ...

  5. 基于springboot的spring JSR validation 后台参数验证

    springboot集成JSR参数校验: 1. 导入maven <dependency> <groupId>org.springframework.boot</group ...

  6. [译]SpringMVC自定义验证注解(SpringMVC custom validation annotations)

    在基于SpringMVC框架的开发中,我们经常要对用户提交的字段进行合法性验证,比如整数类型的字段有个范围约束,我们会用@Range(min=1, max=4).在实际应用开发中,我们经常碰到一些自己 ...

  7. JSR 303标准

    JSR 303是Java为bean数据合法性校验提供的标准框架,JSR 303通过在Bean属性上标注类似于@NotNull.@Max等标准的注解指定校验规则,并通过标准的校验接口对bean进行验证. ...

  8. JSR 303 - Bean Validation 介绍及最佳实践

    JSR 303 - Bean Validation 介绍及最佳实践 JSR 303 – Bean Validation 是一个数据验证的规范,2009 年 11 月确定最终方案.2009 年 12 月 ...

  9. 苦B的程序猿道路数据验证

    发生了什么 再一次苦B程序猿和苦C程序猿结对话发生编程周期 此代码: public void deleteAllExtendAclsFromContent(String contentId) thro ...

随机推荐

  1. CentOS安装glibc-2.14(转)

    到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.xz tar glibc-2.14.tar.gz cd glibc-2.14 mkdir build cd ...

  2. Redis主从配置及通过Keepalived实现Redis自动切换高可用

    Redis主从配置及通过Keepalived实现Redis自动切换高可用 [日期:2014-07-23] 来源:Linux社区  作者:fuquanjun [字体:大 中 小]   一:环境介绍: M ...

  3. Spring 框架的搭建及测试

    1.项目结构如下: 2.com.sxl.pojos==>Student.java package com.sxl.pojos; public class Student { private in ...

  4. Java 连接 Oracle 数据库

    首先要导入ojdbc6.jar 包(放在lib文件夹) 然后需要在数据库建一个student表进行测试: 连接及测试代码: import java.security.interfaces.RSAKey ...

  5. 在SQL Server中调用.NET程序集

    需求是这样的,我在.net程序里操作数据时将一些字段数据加密了,这些数据是很多系统共用的,其中一delphi程序也需要用到,并且需要将数据解密,由于我在.net里加密的方式比较特殊,在delphi程序 ...

  6. MongoDB - Cursors

    db.collection.find()查询集合会返回一个包含查到的文档的游标.在mongo shell中,如果没有定义一个变量来该游标的内容,默认会迭代返回20个文档. > db.users. ...

  7. FIFO、LRU、OPT页面调度算法及样例

    网上非常多介绍3种页面置换算法的样例和过程是不对的, 本文依据<操作系统概念>第七版对三种算法做介绍,并给出正确的样例以验证算法. 一.FIFO先进先出页面置换算法,创建一个FIFO队列来 ...

  8. Oracle学习-Power Designer、visio 2003、Oracle sql developer、OEM、expdp

    Oracle的体系太庞大了.对于刚開始学习的人来说,难免有些无从下手的感觉. 经过一学期的学习对Oracle学习有了一些深入的了解,由于之前学习过Oracle的一些主要的知识.所以学习起来上手比較快一 ...

  9. PowerShell---Operators 介绍

    1.Arithmetic operators(算术运算符) 算术运算符包括加.减.乘.除.取模 此外,加法运算符 (+) 和乘法运算符 (*) 还可对字符串.数组和哈希表进行运算.加法运算符将输入连接 ...

  10. maven分开打包jar文件和依赖jar包和资源文件

    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...