1、说明

学习注解方式之前,应该先学习一下编码方式的spring注入。这样便于理解验证框架的工作原理。在出错的时候,也能更好的解决这个问题。所以本次博客教程也是基于编码方式。仅仅是在原来的基础加上注解方式。

2、配置信息

  • web.xml不须要改变的
  • hello-servlet.xml将原来的载入方式,改为自己主动增加有hibernate和Spring提供的validate的默认类,配置例如以下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan
base-package="controller">
</context:component-scan>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->
</bean> <!--基于validate的载入配置-->
<mvc:annotation-driven validator="validator"/> <!-- <bean id="accountValidator" class="dao.AccountValidator"></bean> --> <bean id= "validator"
class= "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name= "providerClass" value= "org.hibernate.validator.HibernateValidator"/>
<!-- 假设不加默认到 使用classpath下的 ValidationMessages.properties -->
<property name= "validationMessageSource" ref= "messageSource"/>
</bean> <bean id= "messageSource"
class= "org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name= "basename" value= "classpath:message"/>
<property name= "fileEncodings" value= "utf-8"/>
<property name= "cacheSeconds" value= "120"/>
</bean> </beans>
  • 在src文件夹以下新建配置文件message.properties 给文件的载入是由hello-servlet.xml中的字段messageSource中的属性basename的value值决定的。能够不用配置这个文件,可是一般为了支持国际化,可是吧信息写到配置文件里的,例如以下:
account.username.size=\u7528\u6237\u540D\u7684\u957F\u5EA6\u57283-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4
account.username.space=\u7528\u6237\u540D\u5B57\u7B26\u4E32\u4E4B\u95F4\u4E0D\u80FD\u6709\u7A7A\u683C
account.password.size=\u5BC6\u7801\u7684\u957F\u5EA6\u8303\u56F4\u57286-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4

后面的乱码都是转义后生成的。相应的中文例如以下:

3、源码

- 改动Account类,例如以下代码:

package dao;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size; public class Account { @Size(min=3,max=20,message="{account.username.size}")
@Pattern(regexp="^[a-zA-Z0-9]+$",message="{account.username.space}")
private String username; @Size(min=6,max=20,message="{account.password.size}")
private String password; public Account() {
// TODO Auto-generated constructor stub
} public Account(String username, String password) {
super();
this.username = username;
this.password = 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;
} }

当中的@Size @Pattern都是注解方式的数据验证方式,后面的message信息都是定义在message.properties中

  • 不须要AccountValidate类了

3、效果演示

启动tomcat服务,在浏览器中输入:http://localhost:8080/annotation_springmvc/register

我们是用一个大有空格字符串測试一下:



注冊效果:

4、代码位置

假设有须要的,能够去这个位置下载下来看看:注解方式的数据验证。jar包也上传了

Spring MVC 数据验证——validate注解方式的更多相关文章

  1. Spring MVC 数据验证——validate编码方式

    1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...

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

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

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

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

  4. spring mvc数据验证

    今天来说一下.前段验证,与后端数据验证.大家都知道.在我们.注册与登陆的时候,往往需要对数据进行效验.那么前段我们都知道,可以使用,js去做处理. 今天主要讲解.后端的数据效验.这里我们采用Hiber ...

  5. Spring mvc 数据验证

    加入jar包 bean-validator.jar 在实体类中加入验证Annotation和消息提示 package com.stone.model; import javax.validation. ...

  6. 在Spring MVC项目中,注解方式使用 .properties 文件及 UTF-8编码问题

    xml配置 <!-- 配置文件 --> <bean id="configProperties" class="org.springframework.b ...

  7. spring mvc 数据校验(bean实体注解实现)

    spring mvc 数据校验 1.添加个jar (jar与一版本会冲突) <dependency> <groupId>com.fasterxml</groupId> ...

  8. MVC 数据验证

    MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...

  9. MVC 数据验证[转]

    前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...

随机推荐

  1. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

  2. Flexigrid的编辑功能

    editCells:function(){ if(!isEditing){ isEditing = true; $('tbody tr',$(t)).each(function () { for(va ...

  3. JQuery学习(3)

    创建精灵界面导航: 有以下图,合理的布局让图片正确显示: 先写导航栏html代码: <div id="navMenu"> <ul id="spriteN ...

  4. Perl 面向对象编程的两种实现和比较:

    <pre name="code" class="html">https://www.ibm.com/developerworks/cn/linux/ ...

  5. HttpURLConnection中使用代理(Proxy)及其验证(Authentication)

    HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...

  6. 减少可执行程序size的三个常用软件

    减少可执行程序size的三个常用软件 linux下面,直接用strip 这个命令 #:strip xxx 可以去掉编译调试信息和各种符号表,能够大大减小可执行程序size windows下面这种exe ...

  7. 【linux】内核make编译链接相关变量定义

    欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  8. Extjs实现树形结构三连选

    当项目中需要一个部门人员选择或者省市县地域连选时,就需要树形结构的连选. 再此,写了一个简单的树形结构三连选功能,模拟从后台读取数据和处理数据(欢迎大家交流指正). 代码如下: 循环创建三棵树,其中只 ...

  9. 重操JS旧业第七弹:面向对象与对象创建

    JS是一种完全面向对象的程序设计语言,在面向对象处理方面,具有多种多样的实现方式,加之对象成员的动态性使得这门语言更加灵活:而js对象成员动态性也是创建和扩展对象的有力方式. 1 对象成员动态性 属性 ...

  10. 在Centos 5.4上安装Mysql5.5.10 (整理以前的工作文档)

    1.     安装环境 1.1.  目的 安装Mysql5.5.10服务,提供公司XXXX测试环境.正式环境也采用该版本的mysql 1.2. 硬件环境 PC机:IntelE5300 内存4G 硬盘5 ...