SpringMVC之编程式校验
1.编程式效验需要实现Validator接口,针对模型类进行校验
2.jar包
3.项目测试结构
4.StudentModel.java(模型类)
1 package com.wt.entity;
2
3 public class StudentModel {
4 private String name;
5 private String pwd;
6 private int age;
7 public String getName() {
8 return name;
9 }
10 public void setName(String name) {
11 this.name = name;
12 }
13 public String getPwd() {
14 return pwd;
15 }
16 public void setPwd(String pwd) {
17 this.pwd = pwd;
18 }
19 public int getAge() {
20 return age;
21 }
22 public void setAge(int age) {
23 this.age = age;
24 }
25
26 }
5.StudentValidator.java(校验器)
1 package com.wt.entity; 2 3 import org.springframework.validation.Errors; 4 import org.springframework.validation.ValidationUtils; 5 import org.springframework.validation.Validator; 6 7 public class StudentValidator implements Validator{ 8 //验证是否支持效验的StudentModel对象,匹配则执行效验 9 @Override 10 public boolean supports(Class<?> clazz) { 11 12 return StudentModel.class.equals(clazz); 13 } 14 //效验数据的方法,object为效验的对象,Errors为效验的错误信息 15 @Override 16 public void validate(Object obj, Errors errors) { 17 //效验name和pwd属性为空时加入错误信息 18 ValidationUtils.rejectIfEmpty(errors, "name", null,"姓名不能为空"); 19 ValidationUtils.rejectIfEmpty(errors, "pwd", null,"密码不能为空"); 20 if(errors.hasErrors()){ 21 //上面发现错误,直接返回,下面也无需效验 22 return; 23 } 24 //针对属性的规则进行具体效验 25 StudentModel stu=(StudentModel) obj; 26 if(stu.getName().length()<5||stu.getName().length()>10){ 27 errors.rejectValue("name", null, "姓名长度错误,应在5~10间"); 28 } 29 if(stu.getPwd().length()<8||stu.getPwd().length()>15){ 30 errors.rejectValue("pwd", null,"密码长度错误,应在8~15之间"); 31 } 32 if(stu.getAge()<=0||stu.getAge()>100){ 33 errors.rejectValue("age", null, "年龄错误,应在1~100之间"); 34 } 35 } 36 37 }
6.ValidController.java(控制器)
1 package com.wt.entity; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.validation.Errors; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 @Controller//定义控制器 8 public class ValidController { 9 //声明效验器 10 StudentValidator studentValidator=new StudentValidator(); 11 @RequestMapping(value="validstu.html")//访问路径 12 public String validStudent(StudentModel model,Errors errors){ 13 studentValidator.validate(model, errors);//效验对象 14 if(errors.hasErrors()){//发现错误,forword到错误页面 15 return "Error"; 16 } 17 return "result";//成功 18 19 } 20 }
7.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 <!-- 完成视图的对应 -->
18 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
20 <property name="prefix" value="/WEB-INF/jsp/example1/"/>
21 <property name="suffix" value=".jsp"/>
22 </bean>
23
24 </beans>
8.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>SpringMVC</display-name> 4 <servlet> 5 <servlet-name>article6</servlet-name> 6 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 7 <init-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:springmvc-servlet.xml</param-value> 10 </init-param> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>article6</servlet-name> 14 <url-pattern>/url/*</url-pattern> 15 </servlet-mapping> 16 </web-app>
9.Errors.jsp(头部导入form标签)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <sf:form commandName="studentModel"> 12 显示所有错误消息 13 <br> 14 <sf:errors path="*"/> 15 <br> 16 <br> 17 显示属性为age的错误信息 18 <br> 19 <sf:errors path="age"></sf:errors> 20 </sf:form> 21 </body> 22 </html>
10.result.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 效验成功 11 </body> 12 </html>
11.测试结果