Spring的校驗(Validator)


使用Spring校驗的大體流程:

 

最首先要有配置文件xml的支持(spring_validate.xml).(當然在web.xml中要有對該xml的體現)

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="
 6 http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 8 http://www.springframework.org/schema/context
 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
12 ">
13 
14     <bean id="validator"
15         class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
16         <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
17         <!-- 如果不加默認到 使用classpath下的 ValidationMessages.properties -->
18         <property name="validationMessageSource" ref="messageSource" />
19     </bean>
20 
21 
22 
23     <bean id="messageSource"
24         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
25         <!-- 以發布到服務器的路徑為准 -->
26         <property name="basename" value="classpath:messages" />
27         <property name="fileEncodings" value="utf-8" />
28         <property name="cacheSeconds" value="120" />
29     </bean>
30 
31     <bean id="webBindingInitializer"
32         class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
33 
34         <property name="conversionService">
35             <bean
36                 class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
37             </bean>
38 
39         </property>
40         <property name="validator" ref="validator" />
41     </bean>
42 
43 </beans>

 

 

然后就是這個東西登場了:messages.properties.

 

 

 

 

 

然后在model層里相應的地方也要有相應的體現

 1 public class User{    
 2 
 3     @NotEmpty
 4     private String username;
 5     @NotEmpty
 6     private String password;
 7     @NotEmpty
 8     private String repassword;
 9     
10     @NotEmpty
11     @Email
12     private String email;
13     @NotEmpty
14     private String xm;
15 }

 

再然后是control層里的調用:

1 public String xx(@Valid User user,
2         BindingResult result, Map map ){
3         
4         if(result.hasErrors()){
5             return "xx.ftl";
6         }
7 }

 

 

 

最后是在JSP頁面了(個人用的是ftl)

 

首先要有這兩句話:Spring的組件

 1 <#assign spring=JspTaglibs["http://www.springframework.org/tags"]/>

2 <#assign form=JspTaglibs["http://www.springframework.org/tags/form"] /> 

 

然后在對應的input下都要有所體現

1 用戶名:<input type="text" name="username" id="username">
2<@form.errors path="username"/>
3 密碼:<input type="text" name="password" id="password">
4<@form.errors path="password"/>

 

在最后注意form表單要這樣寫。其中commandName是那個的實體類名的小寫。

 

1 <@form.form commandName="user" action="" method="post" name="" id=""> 

 

顯示的效果是這樣:

 

 

 本帖純屬個人的學習和理解,如有錯誤請各位大神指正。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM