上一篇文章分享了,上传和下载。本片文章将分享的内容是 JSR303 以及 拦截器
JSR-303 是 JAVA EE 6 中的一项子规范,叫做 Bean Validation,官方参考实现是Hibernate Validator。
简单的说,JSR-303就是用来做表单验证的。
我们之前用js、jQuery做表单验证,JS属于前端的脚本验证(只作用于客户端的浏览器中,可以被屏蔽)
JSR303属于服务端验证
- <dependency>
- <groupId>org.hibernategroupId>
- <artifactId>hibernate-validatorartifactId>
- <version>6.0.7.Finalversion>
- dependency>
-
- package com.zhw.model;
-
- import javax.validation.constraints.NotBlank;
- import javax.validation.constraints.NotNull;
-
- /**
- * @NotNull :作用于基本数据类型
- * @NotEmpty 作用于集合
- * @NotBlank 作用于字符串
- */
- public class Clazz {
- @NotNull(message = "班级编号不能为空")
- protected Integer cid;
-
- @NotBlank(message = "班级名不能为空")
- protected String cname;
-
- @NotBlank(message = "班级教员老师不能为空")
- protected String cteacher;
-
- protected String pic;
-
- public Clazz(Integer cid, String cname, String cteacher, String pic) {
- this.cid = cid;
- this.cname = cname;
- this.cteacher = cteacher;
- this.pic = pic;
- }
-
- public Clazz() {
- super();
- }
-
- public Integer getCid() {
- return cid;
- }
-
- public void setCid(Integer cid) {
- this.cid = cid;
- }
-
- public String getCname() {
- return cname;
- }
-
- public void setCname(String cname) {
- this.cname = cname;
- }
-
- public String getCteacher() {
- return cteacher;
- }
-
- public void setCteacher(String cteacher) {
- this.cteacher = cteacher;
- }
-
- public String getPic() {
- return pic;
- }
-
- public void setPic(String pic) {
- this.pic = pic;
- }
- }
- package com.zhw.web;
-
- import com.zhw.biz.ClazzBiz;
- import com.zhw.model.Clazz;
- import com.zhw.model.dto.ClazzDto;
- import com.zhw.util.PageBean;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.validation.BindingResult;
- import org.springframework.validation.FieldError;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.multipart.MultipartFile;
-
- import