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. zend_soap做webservice的使用方法

      只用到zend_soap包中的Zend_Soap_Server,Zend_Soap_AutoDiscover和Zend_Soap_Client三个类 首先要注意ZF是调用php的soap扩展,所以 ...

  2. hive整合hbase

    Hive整合HBase后的好处: 通过Hive把数据加载到HBase中,数据源可以是文件也可以是Hive中的表. 通过整合,让HBase支持JOIN.GROUP等SQL查询语法. 通过整合,不仅可完成 ...

  3. ASP.net显示当前系统在线人数

    void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Application.Lock(); if (Applic ...

  4. PHP依赖注入

    对于依赖注入 我现在的理解是把一个方法当成一个变量放进另一个方法的形参里 <?php class Factory { public static function getDb(){ return ...

  5. start 调用外部程序

    批处理中调用外部程序的命令(该外部程序在新窗口中运行,批处理程序继续往下执行,不理会外部程序的运行状况),如果直接运行外部程序则必须等外部程序完成后才继续执行剩下的指令 例:start explore ...

  6. 前端开发-4-HTML-table&form&表单控制 标签

    1.table标签 <!DOCTYPE html> <html lang="cn"> <head> <meta charset=" ...

  7. ABAP-年月期间搜索帮助

    selection-screen begin of block block1 with frame title text-. parameters:p_mon1 like s031-spmon, p_ ...

  8. 运行tomcat显示指定的服务未安装解决办法

    一.问题重现 二.原因分析 tomcat7.exe和tomcat7w.exe要起作用必须先未这两个文件安装服务. 其中tomcat7.exe这个文件是用来启动tomcat的,tomcat7w.exe这 ...

  9. MySql PartionBy

    SELECT tableOld.*, if(@channelName = tableOld.channelName, @rank := @rank + 1, @rank := 1) AS rank, ...

  10. ubuntu ftp 建立匿名用户 [转]

    转自:http://www.cnblogs.com/cocoajin/p/3761414.html ubuntu server vsftpd 匿名用户上传下载及目录设置 1:vsftpd服务器安装: ...