Spring MVC 数据验证——validate注解方式
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注解方式的更多相关文章
- Spring MVC 数据验证——validate编码方式
1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- Spring mvc 数据验证框架注解
@AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为false@DecimalMax(value) 被注解的元素必须为一个数字,其值必须小于等于指定的最小值 ...
- spring mvc数据验证
今天来说一下.前段验证,与后端数据验证.大家都知道.在我们.注册与登陆的时候,往往需要对数据进行效验.那么前段我们都知道,可以使用,js去做处理. 今天主要讲解.后端的数据效验.这里我们采用Hiber ...
- Spring mvc 数据验证
加入jar包 bean-validator.jar 在实体类中加入验证Annotation和消息提示 package com.stone.model; import javax.validation. ...
- 在Spring MVC项目中,注解方式使用 .properties 文件及 UTF-8编码问题
xml配置 <!-- 配置文件 --> <bean id="configProperties" class="org.springframework.b ...
- spring mvc 数据校验(bean实体注解实现)
spring mvc 数据校验 1.添加个jar (jar与一版本会冲突) <dependency> <groupId>com.fasterxml</groupId> ...
- MVC 数据验证
MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...
- MVC 数据验证[转]
前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...
随机推荐
- Android蓝牙A2dp profile的使用
A2dp profile是android支持的一种蓝牙情景模式,一般用于蓝牙立体声耳机,即蓝牙音频的输出 在android的app层中,A2dp的使用并不是很开放,api只提供了非常少的操作接口,连基 ...
- 管理tips
管理是什么? 我认为达到的目的就是高效.低成本. 成本低才能有盈余,才能活的长和舒服.高效就是无谓的消耗少,以结果为导向. 开源节流,应该包含显性的与隐性的两方面. 隐性成本: 1.会议成本;2.沟通 ...
- 设置VIM的配色方案
[转]Ubuntu的VIM的默认颜色难看死了,蓝色的注释基本上看不到. 查看有多少配色方案: $ ls /usr/share/vim/vim72/colors 发现有以下文件 blue.vim ...
- 菜鸟学习SSH(二)——Struts国际化
国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化.程序在不修改内部代 ...
- Python之路day4
坚持就是胜利.今天零下14度,从教室出来的路上真的很冷很冷,希望这个冬天自己不会白过,春暖花开的时候一定要给世界一个更好的自己. 原本以为day3的作业自己做得挺好的,没想到只得了B+.必须要加油了, ...
- IOS 新消息通知提示-声音、震动
一.APNS 1.注册 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificati ...
- 摘抄python __init__
注意1.__init__并不相当于C#中的构造函数,执行它的时候,实例已构造出来了. 1 2 3 4 5 class A(object): def __init__(self,name): ...
- android handler looper thread
在线程中调用包含创建handler方法的时候,会报错,提示: “need call Looper.prepare()” -- 在创建之前,调用Looper.prepare()方法来创建一个looper ...
- javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
在今天之前我一直以为setTimeout这个函数是异步的,无意中看到了一篇关于setTimeout的文章.发现自己曾经的认识全是错误的,赶紧总结下. 先看一段代码: var start = new D ...
- 用反射,将DataRow行转为Object对象
/// <summary> /// 反射辅助类 /// </summary> public class ReflectionHelper { /// <summary&g ...