博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc:数据验证
阅读量:4645 次
发布时间:2019-06-09

本文共 4453 字,大约阅读时间需要 14 分钟。

做学生登录信息必填项验证。

student信息有:id,姓名,年龄

Student.java

public class Student {		private Integer id;	private Integer age;	private String name;		public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}		public Integer getAge() {		return age;	}	public void setAge(Integer age) {		this.age = age;	}		public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}		}

  StudentValidator.java数据验证代码

import org.springframework.stereotype.Service;import org.springframework.validation.Errors;import org.springframework.validation.ValidationUtils;import org.springframework.validation.Validator;@Servicepublic class StudentValidator implements Validator {		public boolean supports(Class
clazz){ return Student.class.isAssignableFrom(clazz); } public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name","Field name is required."); }}

  注意:StudentValidator 需要加@Service注解,否则自动装载会报错,找不到bean

自动注入的时候,找不到对应的bean
原因是:对应要注入的实现类没有 加注解,如dao层 @Repository  如service层  @Service
 
StudentController.java
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.ui.Model;import org.springframework.validation.BindingResult;import org.springframework.validation.Validator;import org.springframework.validation.annotation.Validated;@Controllerpublic class StudentController {		@Autowired		@Qualifier("studentValidator")		private Validator validator;					@InitBinder		private void initBinder(WebDataBinder binder) {		      binder.setValidator(validator);		}			//前端from数据引用:commandName		 @ModelAttribute("student")		   public Student createStudentModel() {    		      return new Student();		   }				@RequestMapping(value = "/student", method = RequestMethod.GET)	   public ModelAndView student() {	      return new ModelAndView("student", "command", new Student());	   }	  	   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)	   public String addStudent(@ModelAttribute("student") @Validated Student student, 	      BindingResult bindingResult, Model model) {	      if (bindingResult.hasErrors()) {	         return "student";	      }	      model.addAttribute("name", student.getName());	      model.addAttribute("age", student.getAge());	      model.addAttribute("id", student.getId());	      return "student_result";	   }			}

  下面来看看jsp代码

student.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ page isELIgnored="false" %>
Spring MVC表单处理

学生信息

姓名:
年龄:
编号:

  

这一块的代码:
<form:form method="POST" action="/hello/addStudent" commandName="student">,其中commandName中的student是,studentController中的createStudentModel的定义.
这一块的代码:<form:errors path="*" cssClass="errorStyle" element="div" />,cssClass为样式代码,element元素为div.
 
student_result.jsp
<%@ page contentType="text/html; charset=UTF-8" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ page isELIgnored="false" %>
spring mvc表单处理

提交的学生信息

名称: ${name}
年龄: ${age}
编号: ${id}

  

转载于:https://www.cnblogs.com/achengmu/p/8892839.html

你可能感兴趣的文章
云计算开发一般负责什么工作呢?云计算是做什么的?
查看>>
[转]Windows Shell 编程 第十二章【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987999】...
查看>>
ubuntu常用技巧积累
查看>>
Java入门第二季——Java中的this关键字
查看>>
MYSQL指令
查看>>
《大道至简》读后感
查看>>
如何优化电量
查看>>
测试用例编写(功能测试框架)
查看>>
问题解决 Visual Studio 2015 无法复制文件“D:\swapfile.sys”
查看>>
eclipse 关联 Maven本地仓库的配置
查看>>
注册表收藏夹路径
查看>>
移动端自动化测试环境搭建
查看>>
【小前端】float属性
查看>>
[Unity插件]Lua行为树(十):通用行为和通用条件节点
查看>>
类StringBuilder
查看>>
212. Word Search II
查看>>
218. The Skyline Problem
查看>>
centos 安装 maven
查看>>
整型与字节转换关系
查看>>
用户名判断(练习)
查看>>