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. 制作可以 SSH 登录的 Docker 镜像

    Docker使用系列一我们把镜像源改为了阿里云的,方便后续的操作. 执行这个命令就把源地址改为阿里的: curl https://git.oschina.net/feedao/Docker_shell ...

  2. Eclipse Debug 使用

      Eclipse的debug模式:代码调试 * Eclipse或MyEclipse就是java的开发工具 * Eclipse开源的.免费的Java开发工具 * MyEclipse基于Eclipse开 ...

  3. Codeigniter 使用 Mysql 存储过程

    本篇文章由:http://xinpure.com/codeigniter-using-mysql-stored-procedures/ 执行存储过程 $query = $this -> db - ...

  4. Java中正数与负数操作>>、>>>的区别

    以下为个人理解,有不对的地方请提出 Java中,>>.>>>都是在数字的二进制的补码中进行的 正数的补码为本身 如33的二进制表示为 00000000 00000000 ...

  5. JS 毫秒日期相互转换 JS获取 今天 明天 昨天的日期

    var dd = new Date(); var AddDayCount = 0; //0 今天 1 明天 -1 昨天 以此类推 dd.setDate(dd.getDate() + AddDayCou ...

  6. DevExpress SpinEdit控件使用实例——删除ASPxSpinEdit右边的上下小箭头SpinButtons-ShowIncrementButtons

    使用ASPxSpinEdit来添加数字类型的输入框: 默认情况下, SpinEdit通过点击右边的上下箭头来加减number,下面的代码是去掉上下箭头,内容居中显示,并将边框颜色置为白色: <d ...

  7. sql getdate()生成单据号

    select replace( replace( replace( replace(convert(varchar(23),getdate(),121),'-',''),':',''),' ','') ...

  8. 阿里云Ubuntu部署java web - 文件夹

    文件夹(点击章节标题阅读): 阿里云Ubuntu部署java web(1) - 系统配置         ssh链接server(使用终端远程链接)        加入用户        给用户赋予运 ...

  9. spring中aop以xml配置方式

    1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...

  10. Certificates

    Certificates Certificates 即 ”证书“,约等于通行证,申请证书是我们进行真机调试与发布的第一步.证书主要分为两类:Development证书用来开发和调试应用程序Produc ...