1. 程式人生 > >SpringMVC之聲明式校驗

SpringMVC之聲明式校驗

分享圖片 -a 前綴 ali hid ssa value ide 後綴

1、在http://www.cnblogs.com/wtzl/p/8830678.html編程式校驗基礎上

2.新增jar包三個

技術分享圖片

3.StudentModel.java(聲明式)

技術分享圖片
 1 package 聲明式校驗;
 2 
 3 import javax.validation.constraints.NotNull;
 4 
 5 import org.hibernate.validator.constraints.Email;
 6 import org.hibernate.validator.constraints.Length;
 7 import org.hibernate.validator.constraints.Range;
8 9 public class StudentModel { 10 @NotNull(message="name不能為空") 11 @Length(min=6,max=10,message="長度是6~10個字符") 12 private String name; 13 @Range(min=10,max=40,message="age在10~40之間") 14 private int age; 15 @Email(message="郵箱地址不正確") 16 private String email; 17 public String getName() {
18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 public String getEmail() { 30 return email;
31 } 32 public void setEmail(String email) { 33 this.email = email; 34 } 35 36 37 }
View Code

4.ValidationAnnotationController .java(控制器簡化)

技術分享圖片
 1 package 聲明式校驗;
 2 
 3 import javax.validation.Valid;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.validation.Errors;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 
 9 @Controller
10 public class ValidationAnnotationController {
11     @RequestMapping("stu.html")
12     public String validStudent(@Valid StudentModel student,Errors errors){
13         if(errors.hasErrors()){
14             return "error2";
15         }
16         return "result";
17     }
18 }
View Code

5.springmvc-servlet.xml(新增兩條屬性)

技術分享圖片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14         <!-- 註解掃描 -->
15     <context:component-scan base-package="com.wt.entity"/>
16     
17      <context:component-scan base-package="聲明式校驗"/>
18      <mvc:annotation-driven></mvc:annotation-driven>
19      
20      
21     <!-- 完成視圖的對應 -->
22     <!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:後綴 -->
23     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
24         <property name="prefix" value="/WEB-INF/jsp/example1/"/>
25         <property name="suffix" value=".jsp"/>
26     </bean>
27 
28 </beans>
View Code

6.jsp文件一樣

7.測試效果

技術分享圖片

SpringMVC之聲明式校驗