生活随笔
收集整理的這篇文章主要介紹了
Spring MVC 数据验证——validate注解方式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、說(shuō)明
學(xué)習(xí)注解方式之前,應(yīng)該先學(xué)習(xí)一下編碼方式的spring注入。這樣便于理解驗(yàn)證框架的工作原理。在出錯(cuò)的時(shí)候,也能更好的解決這個(gè)問(wèn)題。所以本次博客教程也是基于編碼方式。僅僅是在原來(lái)的基礎(chǔ)加上注解方式。
2、配置信息
- web.xml不須要改變的
- hello-servlet.xml將原來(lái)的載入方式,改為自己主動(dòng)增加有hibernate和Spring提供的validate的默認(rèn)類(lèi),配置例如以下:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/contexthttp://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"></bean><mvc:annotation-driven validator="validator"/><bean id= "validator"class= "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"><property name= "providerClass" value= "org.hibernate.validator.HibernateValidator"/><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值決定的。能夠不用配置這個(gè)文件,可是一般為了支持國(guó)際化,可是吧信息寫(xiě)到配置文件里的,例如以下:
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
后面的亂碼都是轉(zhuǎn)義后生成的。相應(yīng)的中文例如以下:
3、源碼
- 改動(dòng)Account類(lèi),例如以下代碼:
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() {}
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;}}
當(dāng)中的@Size @Pattern都是注解方式的數(shù)據(jù)驗(yàn)證方式,后面的message信息都是定義在message.properties中
- 不須要AccountValidate類(lèi)了
3、效果演示
啟動(dòng)tomcat服務(wù),在瀏覽器中輸入:http://localhost:8080/annotation_springmvc/register
我們是用一個(gè)大有空格字符串測(cè)試一下:
注冊(cè)效果:
4、代碼位置
假設(shè)有須要的,能夠去這個(gè)位置下載下來(lái)看看:注解方式的數(shù)據(jù)驗(yàn)證。jar包也上傳了
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的Spring MVC 数据验证——validate注解方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。