码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC之声明式校验

时间:2018-04-14 13:57:09      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:not   ali   setname   base   getname   基础   info   mes   nbsp   

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之声明式校验

标签:not   ali   setname   base   getname   基础   info   mes   nbsp   

原文地址:https://www.cnblogs.com/wtzl/p/8831097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!