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. Textarea自动适用高度且无滚动条解决方案

    今日的系统需要动态显示一项数据库里面的内容,该内容包含换行等格式字符,要求如实的反应在页面上. 最初解决办法是使用textarea控件,代码如下: <textarea style="b ...

  2. PHP基础知识(一)

    The Basics Comparison operators Comparison operators are an often overlooked aspect of PHP, which ca ...

  3. Customize User Interfaces and Pass User Input to Installer Classes

    In this article I am going to demonstrate how to customize your MSI install to prompt the user for s ...

  4. 15个极好的Linux find命令示例

    基于访问/修改/更改时间查找文件 你可以找到基于以下三个文件的时间属性的文件. 访问时间的文件.文件访问时,访问时间得到更新. 的文件的修改时间.文件内容修改时,修改时间得到更新. 更改文件的时间.更 ...

  5. ip范围生成 C#

    #region ip /// <summary> /// ip rang ,ip /// </summary> /// <param name="str&quo ...

  6. Latex排版:CTeX winEdit 输出“系统找不到指定的文件”的解决办法)

    winEdit输出“系统找不到指定的文件”,这里“指定的文件”是“TeXify.exe”等需要运行的程序,而不是当前需要编译的“.tex”文件.所以,问题的本质就是系统找不到“TeXify.exe”等 ...

  7. Ajax高级应用---Comet

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

  8. 记一次.net core调用SOAP接口遇到的问题

    背景        最近需要将一些外部的Web Service及其他SOAP接口的调用移到一个独立的WebAPI项目中,然后供其他.Net Core项目调用.之前的几个Web Service已经成功迁 ...

  9. [emqttd] (EMQ)

    [emqttd] (EMQ)是采用Erlang语言开发,全面支持MQTT V3.1.1协议,支持集群和大规模连接的开源MQTT消息服务器. [emqttd]致力于发布一个基于Erlang/OTP语言平 ...

  10. atitit.javascript js 上传文件的本地预览

    atitit.javascript js 上传文件的本地预览 1. .URL.createObjectURL  1 1.1. 吊销所有使用 URL.createObjectURL 而创建的 URL,以 ...