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. 重载&lt;&lt;和&gt;&gt;

    在C++编程中实现数据的输入/输出能够用cin>>ch/cout<<ch; 可是使用cin>>ch或cout<<ch并不能实现一些特殊的数据的输入或者输 ...

  2. 3dmax做的模型导入U3d后 当模型靠近摄像机时镂空问题

    使用3dMax  Reset XForm下就好了. 原因可能是 法线方向问题?

  3. webDriver API——第7部分Desired Capabilities

    The Desired Capabilities implementation. class selenium.webdriver.common.desired_capabilities.Desire ...

  4. Manual close is not allowed over a Spring managed SqlSession(转)

    最近用junit测试spring项目的时候,报错: Manual close is not allowed over a Spring managed SqlSession 意思是不允许手动关闭spr ...

  5. Vue学习小结

    ES6 let完全可以取代var const声明一个只读的常量 箭头函数:可以绑定this对象,大大减少了显式绑定this对象的写法(call.apply.bind) 函数绑定(function bi ...

  6. Ajax高级应用---Comet

    非常适合处理体育比赛的分数和股票报价 1.HTTP流 将输出缓存中的内容一次性全部发送到客户端的功能是实现HTTP流的关键所在.

  7. jsp自己主动编译机制

    总的来说,Jasper的自己主动检測实现的机制比較简单,依靠某后台线程不断检測JSP文件与编译后的class文件的最后改动时间是否同样,若同样则觉得没有改动.但倘若不同则须要又一次编译.实际上因为在T ...

  8. 将Cmder添加到系统右键菜单中

    1.把 Cmder 加到环境变量 把Cmder.exe存放的目录添加到系统环境变量: 加完之后,Win+r一下输入cmder,即可. 2.添加 cmder 到右键菜单:环境变量添加后,在任意文件夹中即 ...

  9. log4cxx在linux下的编译使用

    最近在linux下使用log4cxx库,按照其官方文档提供的方法来进行编译,不能成功,又利用google搜索了好几个中文博客上讲述在linux下编译使用log4cxx库的方法,依然不能成功,在这里我奉 ...

  10. andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not fou

    1.加证书(这里不说) 2.修改代码 import java.security.KeyManagementException;import java.security.NoSuchAlgorithmE ...