标签: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 }
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 }
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>
6.jsp文件一样
7.测试效果
标签:not ali setname base getname 基础 info mes nbsp
原文地址:https://www.cnblogs.com/wtzl/p/8831097.html