1、maven

 <dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1..Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2..Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.</version>
</dependency>

2、修改配置文件applicationContex.xml

 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!--不设置则默认为classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="validatemessageSource"/>
</bean>
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validatemessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value=""/>
</bean>
<mvc:annotation-driven validator="validator" />
validator="validator"是上面定义的id

3、新建validationMessages.properties 文件放到resources文件夹中,内容如下

name.not.empty=

111111内容可自定义

4、新建实体类

package com.db.pojo;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty; public class User { @NotEmpty(message="{name.not.empty}")
private String userName; private String password; public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
name.not.empty 为validationMessages.properties里面配置的名称
@NotEmpty代表不能为空,可从网上查询其它验证标签

5、查看controller
    @RequestMapping(value="/userlogin", method = {RequestMethod.GET})
public String userLogin(Model model){ if(!model.containsAttribute("contentModel")){
model.addAttribute("contentModel", new User());
}
return "userlogin";
} @RequestMapping(value="/userlogin", method = {RequestMethod.POST})
public String userLogin(@Valid @ModelAttribute("contentModel") User user, BindingResult result){ Map<String, Object> map = new HashMap<String, Object>();
if(result.hasErrors()){
return "/userlogin";
}
else {
return "/index";
} }

6、jsp页面

<%@ page language="java"  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html>
<head>
<title>Reservation Form</title> <style>
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head> <body>
<form:form method="post" modelAttribute="contentModel" action="userlogin">
<form:errors path="*" cssClass="error" />
<table>
<tr>
<td>Name</td>
<td><form:input path="userName" />
</td>
<td><form:errors path="userName" cssClass="error" />
</td>
</tr>
<tr>
<td>password</td>
<td><form:input path="password" />
</td>
<td><form:errors path="password" cssClass="error" />
</td>
</tr> <tr>
<td colspan=""><input type="submit" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
访问http://****/userlogin 点击submit 按钮会提示 1111111(就是validationMessages.properties文件中配置的内容)

springmvc 整合数据验证框架 jsr的更多相关文章

  1. Hibernate Validation,Spring mvc 数据验证框架注解

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  2. struts2(三)---struts2中的服务端数据验证框架validate

    struts2为我们提供了一个很好的数据验证框架–validate,该框架可以很方便的实现服务端的数据验证. ActionSupport类提供了一个validate()方法,当我们需要在某一个acti ...

  3. 15.SpringMVC核心技术-数据验证

    在 Web 应用程序中,为了防止客户端传来的数据引发程序的异常,常常需要对数据进行验证. 输入验证分为客户端验证与服务器端验证.客户端验证主要通过 JavaScript 脚本进 行, 而服务器端验证则 ...

  4. SpringMVC学习笔记七:SpringMVC的数据验证

    SpringMVC支持JSR(Java Specification Requests, Java规范提案)303-Bean Validation数据验证规范,该规范的实现者很多,其中较常用的是 Hib ...

  5. SpringMVC配置数据验证(JSR-303)

    这篇文章已经过时了. 请参考比较合适的前后端交互方式. 1.pom.xml中追加hibernate-validator 2.在dto类的域上追加JSR-303的注解 public class Data ...

  6. SpringMVC整合Shiro权限框架

    尊重原创:http://blog.csdn.net/donggua3694857/article/details/52157313 最近在学习Shiro,首先非常感谢开涛大神的<跟我学Shiro ...

  7. SpringMVC整合Shiro安全框架(一)

    一. 准备工作 1. 本文参考自张开涛的 <跟我学Shiro> 二. 简介 1. Apache Shiro是Java的一个安全框架.可以帮助我们完成:认证.授权.加密.会话管理.与Web集 ...

  8. Spring mvc 数据验证框架注解

    @AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为false@DecimalMax(value) 被注解的元素必须为一个数字,其值必须小于等于指定的最小值 ...

  9. springMVC数据验证出现404错误解决办法

    今天使用springMVC的数据验证的时候,看似很简单的东西,却有一个很大的陷阱:提交空表单的时候总是出现404错误,但是后台却不给你报任何错.遇到这个错误这个很苦恼,搞了几小时,今天记录并分享一下解 ...

随机推荐

  1. selenium ie 模拟request pahonjs

  2. python聚合云图

    今天一时兴起,想用python爬爬自己的博客,通过数据聚合,制作高逼格的云图(对词汇出现频率视觉上的展示),看看最近我到底写了啥文章. 一.直接上几张我的博客数据的云图 1.1 爬取文章的标题的聚合 ...

  3. (19/24) webpack实战技巧:推荐使用的第三方类库打包方法

    在日常的开发中,总避免不了引入第三方的框架,比如常用的JQuery,此节我们来学习一下如何优雅并正确的用webpack引入第三方库. 这里我们以第三方框架JQuery为例: 1.在入口文件中引入 1. ...

  4. saltstack安装配置及常用命令

    1.salt安装及配置详解 https://www.cnblogs.com/lgeng/p/6567424.html centos7配置: https://www.jianshu.com/p/4c91 ...

  5. leetcode405

    public class Solution { string ConverToHex(string bit) { var s = ""; switch (bit) { " ...

  6. Eclipse 中yml自动提示功能相关设置

    转自:https://blog.csdn.net/lililuni/article/details/82849376

  7. windows系统安装参数

    winnt32 /dudisable /s:. /unattend:winnt.sif /makelocalsource /tempdrive:C /syspart:C 以上各参数解释如下: /dud ...

  8. JS 时间 获取 当天,昨日,本周,上周,本月,上月

    调用 setTimeRange (2); function  setTimeRange (v) { var fmt = 'YYYY-MM-DD HH:mm'; var now = new Date() ...

  9. ICE中间件相关

    Ice 是 网络通信引擎 Internet Communications Engine 的简称,是ZeroC开发的一个面向对象的中间件平台.它提供了面向对象的远程过程调用.网格计算和发布/订阅功能,并 ...

  10. sql查询分析器中显示行号

    -- 工具-> -- 选项-> -- 文本编辑器-> -- 所有语言-> -- 常规-> -- 显示-> -- 行号