1.校验框架
1.表单校验框架入门
表单校验的重要性
数据可以随意输入,导致错误的结果。表单校验保障了数据有效性、安全性
表单校验分类
校验位置:
客户端校验
服务端校验
校验内容与对应方式:
格式校验
客户端:使用Js技术,利用正则表达式校验
服务端:使用校验框架
逻辑校验
客户端:使用ajax发送要校验的数据,在服务端完成逻辑校验,返回校验结果
服务端:接收到完整的请求后,在执行业务操作前,完成逻辑校验
表单校验规则
长度:例如用户名长度,评论字符数量
非法字符:例如用户名组成
数据格式:例如Email格式、 IP地址格式
边界值:例如转账金额上限,年龄上下限
重复性:例如用户名是否重复
表单校验框架
JSR(Java Specification Requests):Java 规范提案
303:提供bean属性相关校验规则
JCP(Java Community Process):Java社区
Hibernate框架中包含一套独立的校验框架hibernate-validator
导入坐标
<!--导入校验的jar303规范--><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!--导入校验框架实现技术--><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>6.1.0.Final</version></dependency>
注意:
tomcat7 :搭配hibernate-validator版本5...Final
tomcat8.5↑ :搭配hibernate-validator版本6...Final
1.开启校验
名称:@Valid 、 @Validated
类型:形参注解
位置:处理器类中的实体类类型的方法形参前方
作用:设定对当前实体类类型参数进行校验
@RequestMapping(value = "/addemployee")
public String addEmployee(@Valid Employee employee) {System.out.println(employee);
}
2.设置校验规则
名称:@NotNull
类型:属性注解 等
位置:实体类属性上方
作用:设定当前属性校验规则
每个校验规则所携带的参数不同,根据校验规则进行相应的调整
具体的校验规则查看对应的校验框架进行获取
public class Employee{@NotNull(message = "姓名不能为空")private String name;//员工姓名
}
3.获取错误信息
@RequestMapping(value = "/addemployee")
public String addEmployee(@Valid Employee employee, Errors errors, Model model){System.out.println(employee);if(errors.hasErrors()){for(FieldError error : errors.getFieldErrors()){model.addAttribute(error.getField(),error.getDefaultMessage());}return "addemployee.jsp";}return "success.jsp";
}
通过形参Errors获取校验结果数据,通过Model接口将数据封装后传递到页面显示
<form action="/addemployee" method="post">员工姓名:<input type="text" name="name"><span style="color:red">${name}</span><br/>员工年龄:<input type="text" name="age"><br/><input type="submit" value="提交">
</form>
通过形参Errors获取校验结果数据,通过Model接口将数据封装后传递到页面显示
页面获取后台封装的校验结果信息
完整代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com</groupId><artifactId>MVCvalidator</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.19.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.22</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.13</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.7</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version></dependency><!--servlet3.1规范的坐标--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!--jsp坐标--><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency><!--spring web坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.1.9.RELEASE</version></dependency><!--springmvc坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><!--json相关坐标3个--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.0</version></dependency><!--导入校验的jar303规范--><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!--导入校验框架实现技术--><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>6.1.0.Final</version></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--构建--><!-- <build><!–设置插件–><plugins><!–具体的插件配置–><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>80</port><path>/</path></configuration></plugin></plugins></build>--></project>
web.xml
<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>数据添加成功</h1>
addemployee.xml
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>添加员工-用于演示表单验证</title>
</head>
<body><form action="/addemployee" method="post">员工姓名:<input type="text" name="name"><span style="color: red">${name}</span><br>员工年龄:<input type="text" name="age"><br><input type="submit" name="提交"></form>
</body>
</html>
package com.controller;import com.domain.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;import javax.validation.Valid;
import java.util.List;@Controller
public class EmployeeController {@RequestMapping(value = "/addemployee")public String addEmployee(@Valid Employee employee, Errors errors, Model m){if(errors.hasErrors()){List<FieldError> fieldErrors = errors.getFieldErrors();for(FieldError error:fieldErrors){
// System.out.println(error.getField());
// System.out.println(error.getDefaultMessage());m.addAttribute(error.getField(),error.getDefaultMessage());}return "addemployee.jsp";}return "success.jsp";}
}
package com.domain;import javax.validation.constraints.NotBlank;public class Employee {@NotBlank(message = "姓名不能为空")private String name; //员工姓名private Integer age; //员工年龄public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", age=" + age +'}';}
}
2.多规则校验
同一个属性可以添加多个校验器
@NotNull(message = "请输入您的年龄")
@Max(value = 60,message = "年龄最大值不允许超过60岁")
@Min(value = 18,message = "年龄最小值不允许低于18岁")
private Integer age;//员工年龄
3种判定空校验器(NotNull)的区别
表单数据 | @NotNull | @NotEmpty | @NotBlank |
---|---|---|---|
String s = null; | false | false | false |
String s = “”; | true | false | false |
String s = " "; | true | true | false |
String s = “test”; | true | true | true |
3.嵌套校验
名称:@Valid
类型:属性注解
位置:实体类中的引用类型属性上方
作用:设定当前应用类型属性中的属性开启校验
public class Employee {//实体类中的引用类型通过标注@Valid注解,设定开启当前引用类型字段中的属性参与校验@Validprivate Address address;
}
注意:开启嵌套校验后,被校验对象内部需要添加对应的校验规则
4.分组校验
同一个模块,根据执行的业务不同,需要校验的属性会有不同
新增用户
修改用户
对不同种类的属性进行分组,在校验时可以指定参与校验的字段所属的组类别
定义组(通用)
为属性设置所属组,可以设置多个
开启组校验
public interface GroupOne {
}
@RequestMapping(value = "/addemployee")public String addEmployee(@Validated({GroupA.class}) Employee employee, Errors errors, Model m){if(errors.hasErrors()){List<FieldError> fieldErrors = errors.getFieldErrors();System.out.println(fieldErrors.size());for(FieldError error:fieldErrors){
// System.out.println(error.getField());
// System.out.println(error.getDefaultMessage());m.addAttribute(error.getField(),error.getDefaultMessage());}return "addemployee.jsp";}return "success.jsp";}
@NotEmpty(message = "姓名不能为空",groups = {GroupOne.class})
private String name;//员工姓名
2.文件上传
1.文件上传下载
上传文件过程分析
SpringMvc把这些步骤进行封装变为一个接口,就是MultipartResolver
MultipartResolver接口
MultipartResolver接口定义了文件上传过程中的相关操作,并对通用性操作进行了封装
MultipartResolver接口底层实现类CommonsMultipartResovler
CommonsMultipartResovler并未自主实现文件上传下载对应的功能,而是调用了apache的文件上传下载组件
需导入坐标
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version>
</dependency>
代码实现
package com.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.IOException;@Controller
public class FileUploadController {@RequestMapping(value = "/fileupload")public String fileupload(MultipartFile file) throws IOException {System.out.println("file upload is running..."+file);file.transferTo(new File("zjl.png"));return "page.jsp";}
}
<context:component-scan base-package="com"/><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="1024000000"/></bean><mvc:annotation-driven/>
fileupload.jsp
<form action="/fileupload" method="post" enctype="multipart/form-data">上传LOGO:<input type="file" name="file"/><br/>上传照片:<input type="file"/><br/>上传任意文件:<input type="file"/><br/><input type="submit" value="上传"/>
</form>
2.文件上传注意事项
1.文件命名问题, 获取上传文件名,并解析文件名与扩展名
2.文件名过长问题
3.文件保存路径
4.重名问题
package com.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;@Controller
public class FileUploadController {@RequestMapping(value = "/fileupload")
//参数中定义MultipartFile参数,用于接收页面提交的type=file类型的表单,要求表单名称与参数名相同public String fileupload(MultipartFile file,MultipartFile file1,MultipartFile file2, HttpServletRequest request) throws IOException {System.out.println("file upload is running ..."+file);// MultipartFile参数中封装了上传的文件的相关信息
// System.out.println(file.getSize());
// System.out.println(file.getBytes().length);
// System.out.println(file.getContentType());
// System.out.println(file.getName());
// System.out.println(file.getOriginalFilename());
// System.out.println(file.isEmpty());//首先判断是否是空文件,也就是存储空间占用为0的文件if(!file.isEmpty()){//如果大小在范围要求内正常处理,否则抛出自定义异常告知用户(未实现)//获取原始上传的文件名,可以作为当前文件的真实名称保存到数据库中备用String fileName = file.getOriginalFilename();//设置保存的路径String realPath = request.getServletContext().getRealPath("/images");//保存文件的方法,指定保存的位置和文件名即可,通常文件名使用随机生成策略产生,避免文件名冲突问题file.transferTo(new File(realPath,file.getOriginalFilename()));}//测试一次性上传多个文件if(!file1.isEmpty()){String fileName = file1.getOriginalFilename();//可以根据需要,对不同种类的文件做不同的存储路径的区分,修改对应的保存位置即可String realPath = request.getServletContext().getRealPath("/images");file1.transferTo(new File(realPath,file1.getOriginalFilename()));}if(!file2.isEmpty()){String fileName = file2.getOriginalFilename();String realPath = request.getServletContext().getRealPath("/images");file2.transferTo(new File(realPath,file2.getOriginalFilename()));}return "page.jsp";}}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<form action="/fileupload" method="post" enctype="multipart/form-data">上传LOGO:<input type="file" name="file"/><br/>上传照片:<input type="file1"/><br/>上传任意文件:<input type="file2"/><br/><input type="submit" value="上传"/>
</form>
3.Restful
1.Restful开发入门
Rest( REpresentational State Transfer) 一种网络资源的访问风格,定义了网络资源的访问方式
传统风格访问路径
http://localhost/user/get?id=1
http://localhost/deleteUser?id=1
Rest风格访问路径
http://localhost/user/1
Restful是按照Rest风格访问网络资源
优点
隐藏资源的访问行为,通过地址无法得知做的是何种操作
书写简化
Rest行为约定方式
GET(查询) http://localhost/user/1 GET
POST(保存) http://localhost/user POST
PUT(更新) http://localhost/user PUT
DELETE(删除) http://localhost/user DELETE
注意:上述行为是约定方式,约定不是规范,可以打破,所以称Rest风格,而不是Rest规范
2.Restful风格配置
package com.controller;import org.springframework.web.bind.annotation.*;//设置rest风格的控制器
@RestController
//设置公共访问路径,配合下方访问路径使用
@RequestMapping("/user/")
public class UserController {//rest风格访问路径完整书写方式@RequestMapping("/user/{id}")//使用@PathVariable注解获取路径上配置的具名变量,该配置可以使用多次public String restLocation(@PathVariable Integer id){System.out.println("restful is running ....");return "success.jsp";}//rest风格访问路径简化书写方式,配合类注解@RequestMapping使用@RequestMapping("{id}")public String restLocation2(@PathVariable Integer id){System.out.println("restful is running ....get:"+id);return "success.jsp";}//接收GET请求配置方式@RequestMapping(value = "{id}",method = RequestMethod.GET)//接收GET请求简化配置方式@GetMapping("{id}")public String get(@PathVariable Integer id){System.out.println("restful is running ....get:"+id);return "success.jsp";}//接收POST请求配置方式@RequestMapping(value = "{id}",method = RequestMethod.POST)//接收POST请求简化配置方式@PostMapping("{id}")public String post(@PathVariable Integer id){System.out.println("restful is running ....post:"+id);return "success.jsp";}//接收PUT请求简化配置方式@RequestMapping(value = "{id}",method = RequestMethod.PUT)//接收PUT请求简化配置方式@PutMapping("{id}")public String put(@PathVariable Integer id){System.out.println("restful is running ....put:"+id);return "success.jsp";}//接收DELETE请求简化配置方式@RequestMapping(value = "{id}",method = RequestMethod.DELETE)//接收DELETE请求简化配置方式@DeleteMapping("{id}")public String delete(@PathVariable Integer id){System.out.println("restful is running ....delete:"+id);return "success.jsp";}
}
page.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>数据添加成功</h1>
<form action="/user/1" method="post"><input type="text" name="_method" value="PUT"><input type="submit"/>
</form>
开启SpringMVC对Restful风格的访问支持过滤器,即可通过页面表单提交PUT与DELETE请求
页面表单使用隐藏域提交请求类型,参数名称固定为_method,必须配合提交类型method=post使用
在web.xml中配置过滤器
<!--配置过滤器,解析请求中的参数_method,即可通过页面表单提交PUT请求与DELETE请求-->
<filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><servlet-name>DispatcherServlet</servlet-name>
</filter-mapping>
3.postman使用
直接在官网下载,然后安装注册
postman 是一款可以发送Restful风格请求的工具,方便开发调试