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 指定允许的长度 指 ...
随机推荐
- 杭电1874畅通project绪
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 关于PhpDE zend ide破解方式
1.文件和汉化文件 ZendStudio官方下载地址:http://www.geekso.com/component/zendstudio-downloads/ 百度云地址: 10.0.0.msi文件 ...
- PyDev下PyQt 的尝试
刚刚装完PyDev ,试了下之前写的调用PyQt的下代码,发现运行出错:搜索只还需在System PYHONPATH下 添加PyQt的路径,步骤如下: eclipse--window--Prefere ...
- 获取操作系统版本Asp.Net
/// <summary> /// 获取操作系统版本号 /// </summary> /// <returns></returns> public st ...
- Windows Azure 社区新闻综述(#78 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话.以下是本周的亮点. 博客文章: 博客:Windows Azure BizTalk 服务:如何开 ...
- 基于visual Studio2013解决C语言竞赛题之0702函数设计
题目
- while和do while习题
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习 { ...
- C#中对文件的操作
详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801 源码举例: public class FileSystemManager { ...
- UML03-类图
1.在类图中,聚合关系表达总体与局部的关系. 2.请根据下面的需求,画出用例图和类图. 系统允许管理员通过磁盘加载存货数据来运行存货清单报告: 管理员通过从磁盘加载存货数据.向磁盘保存存货数据来更新存 ...
- 基于visual Studio2013解决C语言竞赛题之1022最大数最小数
题目 解决代码及点评 /************************************************************************/ ...