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. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何在同一台PC上运行多个TwinCAT程序

    右击桌面右下角的TC2图标,切换到PLC Configuration,然后在Plc Settings中设置数量为4(TC2最多可以运行的数量是4个),然后点击Apply   可能需要输入登录用户名和密 ...

  2. 病毒木马查杀实战第015篇:U盘病毒之脱壳研究

    前言 因为我们的终于目标是编写出针对于这次的U盘病毒的专杀工具.而通过上次的分析我们知道,病毒有可能在不同的计算机中会以不同的名称进行显示.假设真是如此,那么就有必要在此分析出病毒的命名规律等特征,然 ...

  3. ASP.NET MVC & Web API Brief Introduction

    Pure Web Service(ASMX): Starting back in 2002 with the original release of .NET, a developer could f ...

  4. jquery操作select2控件

    (一)select2常用的操作:添加.移除.获取选中的value()与text() (1)移除事件:$("#select_id").unbind("change" ...

  5. 基于paramiko进行远程执行Linux命令

    直接贴一段代码import paramiko class remote_start(object): def __init__(self,host,username,pwd): self.ssh = ...

  6. Unity3d 录像

    flashtd1: 回复 tqfa :我觉得是有方法可以实现的,之前使用高通的增强显示开发包时发现其实它是添加了一个类似movietexture的东西,叠加在它的摄像机上 如果文档里有操作moviet ...

  7. Ubuntu 下apache2 增加新的module

    http://andrew913.iteye.com/blog/398648 首先来介绍下apache的一个工具apxs.apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译 ...

  8. cocos2dx 制作单机麻将(四)

    cocos2dx 制作单机麻将(四) 麻将逻辑5.模拟出牌 // // main.cpp // MajiangLogicTest // // Created by TinyUlt on 14-8-16 ...

  9. init: cannot execve(‘XXX’):Permission denied问题

    近期在给android 4.3系统进行root时候,一直出现例如以下的红色权限问题  :  7.695741] Freeing init memory: 236K [    8.555286] ini ...

  10. jrebel 7免费激活(非破解) 和 IntelliJ Idea 2017 免费激活方法

    转自http://www.cnblogs.com/suiyueqiannian/p/6734412.html http://www.cnblogs.com/suiyueqiannian/p/67540 ...