现在网络大多数教程都是直接上手springMVC,需要编写大量重复而又既定的配置文件。本次我们使用springBoot来进行springMVC的学习,使用springBoot可以节省掉很多编写配置文件的时间。springboot自动实现spring容器的托管,并且在application.yaml
中对编码方式进行配置可以省去编码过滤器的配置,防止乱码,并且可以简化视图解析器的配置。
阅读前需要有以下预备知识:
- JavaWeb
- 简单了解File类
- IO流的简单使用
Spring MVC
Spring MVC 是目前主流的实现 MVC 设计模式的企业级开发框架,Spring 框架的一个子模块,无需整合,开发起来更加便捷。
什么是 MVC 设计模式?
将应用程序分为 Controller、Model、View 三层,Controller 接收客户端请求,调用 Model 生成业务数据,传递给 View。
Spring MVC 就是对这套流程的封装,屏蔽了很多底层代码,开放出接口,让开发者可以更加轻松、便捷地完成基于 MVC 模式的 Web 开发。
Spring MVC 的核心组件
- DispatcherServlet:前置控制器,是整个流程控制的核心,控制其他组件的执行,进行统一调度,降低组件之间的耦合性,相当于总指挥。
- Handler:处理器,完成具体的业务逻辑,相当于 Servlet 或 Action。
- HandlerMapping:DispatcherServlet 接收到请求之后,通过 HandlerMapping 将不同的请求映射到不同的 Handler。
- HandlerInterceptor:处理器拦截器,是一个接口,如果需要完成一些拦截处理,可以实现该接口。
- HandlerExecutionChain:处理器执行链,包括两部分内容:Handler 和 HandlerInterceptor(系统会有一个默认的 HandlerInterceptor,如果需要额外设置拦截,可以添加拦截器)。
- HandlerAdapter:处理器适配器,Handler 执行业务方法之前,需要进行一系列的操作,包括表单数据的验证、数据类型的转换、将表单数据封装到 JavaBean 等,这些操作都是由 HandlerApater 来完成,开发者只需将注意力集中业务逻辑的处理上,DispatcherServlet 通过 HandlerAdapter 执行不同的 Handler。
- ModelAndView:装载了模型数据和视图信息,作为 Handler 的处理结果,返回给 DispatcherServlet。
- ViewResolver:视图解析器,DispatcheServlet 通过它将逻辑视图解析为物理视图,最终将渲染结果响应给客户端。
Spring MVC 的工作流程
- 客户端请求被 DisptacherServlet 接收。
- 根据 HandlerMapping 映射到 Handler。
- 生成 Handler 和 HandlerInterceptor。
- Handler 和 HandlerInterceptor 以 HandlerExecutionChain 的形式一并返回给 DisptacherServlet。
- DispatcherServlet 通过 HandlerAdapter 调用 Handler 的方法完成业务逻辑处理。
- Handler 返回一个 ModelAndView 给 DispatcherServlet。
- DispatcherServlet 将获取的 ModelAndView 对象传给 ViewResolver 视图解析器,将逻辑视图解析为物理视图 View。
- ViewResovler 返回一个 View 给 DispatcherServlet。
- DispatcherServlet 根据 View 进行视图渲染(将模型数据 Model 填充到视图 View 中)。
- DispatcherServlet 将渲染后的结果响应给客户端。
Spring MVC 流程非常复杂,实际开发中很简单,因为大部分的组件不需要开发者创建、管理,只需要通过配置文件的方式完成配置即可,真正需要开发者进行处理的只有 Handler 、View。
创建SpringBoot工程
输入http://start.spring.io,这一步最好有梯子,不然会报错失败
勾选spring web,以及Lombok
如何使用?
- 创建 Handler
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloHandler {
@RequestMapping("/index")
public String index(){
System.out.println("执行了index...");
return "index";
}
}
运行启动类,在浏览器中输入http://localhost:8081/index即可访问网址,自动内嵌了tomcat以及各种资源配置,对于我们学习springMVC非常方便。
乱码问题
在每个jsp文件开头加上以下的代码配置,即可解决页面内显示乱码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
对于中文数据数据传输乱码问题,我们需要在Resources
目录下新建application.yaml
,并写入如下配置
server:
port: 8081
tomcat:
uri-encoding: UTF-8
spring:
mvc:
view:
prefix: /
suffix: .jsp
http:
encoding:
charset: utf-8
force: true
enabled: true
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
规定端口,编码方式,视图解析器,以及文件上传、传输大小
如何添加 JSP/html 文件
将html文件或者jsp文件放入Resources/templates
并且配置application.yaml
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
mode: HTML
之后就可以用视图解析器解析
Spring MVC 注解
@RequestMapping
Spring MVC 通过 @RequestMapping 注解将 URL 请求与业务方法进行映射,在 Handler 的类定义处以及方法定义处都可以添加 @RequestMapping ,在类定义处添加,相当于客户端多了一层访问路径。
@Controller
@Controller 在类定义处添加,将该类交个 IoC 容器来管理(结合 springmvc.xml 的自动扫描配置使用),同时使其成为一个控制器,可以接收客户端请求。
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@RequestMapping("/index")
public String index(){
System.out.println("执行了index...");
return "index";
}
}
@RequestMapping
相关参数
1、value:指定 URL 请求的实际地址,是 @RequestMapping 的默认值。
@RequestMapping("/index")
public String index(){
System.out.println("执行了index...");
return "index";
}
等于
@RequestMapping(value="/index")
public String index(){
System.out.println("执行了index...");
return "index";
}
2、method:指定请求的 method 类型,GET、POST、PUT、DELET。
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index(){
System.out.println("执行了index...");
return "index";
}
上述代码表示 index 方法只能接收 GET 请求。
3、params:指定请求中必须包含某些参数,否则无法调用该方法。
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"name","id=10"})
public String index(){
System.out.println("执行了index...");
return "index";
}
上述代码表示请求中必须包含 name 和 id 两个参数,同时 id 的值必须是 10。
关于参数绑定,在形参列表中通过添加 @RequestParam
注解完成 HTTP 请求参数与业务方法形参的映射,除了restful风格外,一般这种方法较多
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"name","id=10"})
public String index(@RequestParam("name") String str,@RequestParam("id") int age){
System.out.println(str);
System.out.println(age);
System.out.println("执行了index...");
return "index";
}
上述代码表示将请求的参数 name 和 id 分别赋给了形参 str 和 age ,同时自动完成了数据类型转换,将 “10” 转为了 int 类型的 10,再赋给 age,这些工作都是由 HandlerAdapter 来完成的。
Spring MVC 也支持 RESTful 风格的 URL。
传统类型:http://localhost:8080/hello/index?name=zhangsan&id=10
REST:http://localhost:8080/hello/index/zhangsan/10
@RequestMapping("/rest/{name}/{id}")
public String rest(@PathVariable("name") String name,@PathVariable("id") int id){
System.out.println(name);
System.out.println(id);
return "index";
}
通过 @PathVariable
注解完成请求参数与形参的映射。
- 映射 Cookie
Spring MVC 通过映射可以直接在业务方法中获取 Cookie 的值。
@RequestMapping("/cookie")
public String cookie(@CookieValue(value = "JSESSIONID") String sessionId){
System.out.println(sessionId);
return "index";
}
- JSP 页面的转发和重定向:
Spring MVC 默认是以转发的形式响应 JSP。
1、转发 输入http://localhost:8081/hello/forward
@RequestMapping("/forward")
public String forward(){
return "forward:/index.jsp";
// return "index";
}
2、重定向 输入http://localhost:8081/hello/redirect
@RequestMapping("/redirect")
public String redirect(){
return "redirect:/index.jsp";
}
Spring MVC 数据绑定(页面数据传到控制类)
- 使用 JavaBean 绑定参数
Spring MVC 会根据请求参数名和 JavaBean 属性名进行自动匹配,自动为对象填充属性值,同时支持及联属性。
- 把类当做控制器函数的参数,传入参数的名称相同与该类的成员变量自动匹配
- 把该类当做数据
package com.southwind.entity;
import lombok.Data;
@Data
public class Address {
private String value;
}
package com.southwind.entity;
import lombok.Data;
@Data
public class User {
private long id;
private String name;
private Address address;
}
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2019-03-13
Time: 15:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/save" method="post">
用户id:<input type="text" name="id"/><br/>
用户名:<input type="text" name="name"/><br/>
用户地址:<input type="text" name="address.value"/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String save(User user){
System.out.println(user);
return "index";
}
(将register.jsp放到webapp目录即可)
数据绑定:在后端的业务方法中直接获取客户端 HTTP 请求中的参数,将请求参数映射到业务方法的形参中,Spring MVC 中数据绑定的工作是由 HandlerAdapter 来完成的。
-
基本数据类型
使用方法http://localhost:8081/data/baseType?id=1,之后会把id的值传进来
@RequestMapping("/baseType")
@ResponseBody
public String baseType(int id){
return id+"";
}
@ResponseBody 表示 Spring MVC 会直接将业务方法的返回值响应给客户端,如果不加 @ResponseBody 注解,Spring MVC 会将业务方法的放回值传递给 DispatcherServlet,再由 DisptacherServlet 调用 ViewResolver 对返回值进行解析,映射到一个 JSP 资源。
- 包装类
@RequestMapping("/packageType")
@ResponseBody
public String packageType(@RequestParam(value = "num",required = false,defaultValue = "0") Integer id){
return id+"";
}
包装类可以接收 null,当 HTTP 请求没有参数时,使用包装类定义形参的数据类型,程序不会抛出异常。
@RequestParam
value = "num"
:将 HTTP 请求中名为 num 的参数赋给形参 id。
requried
:设置 num 是否为必填项,true 表示必填,false 表示非必填,可省略。
defaultValue = “0”
:如果 HTTP 请求中没有 num 参数,默认值为0.
- 数组
@RestController
@RequestMapping("/data")
public class DataBindHandler {
@RequestMapping("/array")
public String array(String[] name){
String str = Arrays.toString(name);
return str;
}
}
@RestController 表示该控制器会直接将业务方法的返回值响应给客户端,不进行视图解析。
@Controller 表示该控制器的每一个业务方法的返回值都会交给视图解析器进行解析,如果只需要将数据响应给客户端,而不需要进行视图解析,则需要在对应的业务方法定义处添加 @ResponseBody。
@RestController
@RequestMapping("/data")
public class DataBindHandler {
@RequestMapping("/array")
public String array(String[] name){
String str = Arrays.toString(name);
return str;
}
}
等同于
@Controller
@RequestMapping("/data")
public class DataBindHandler {
@RequestMapping("/array")
@ResponseBody
public String array(String[] name){
String str = Arrays.toString(name);
return str;
}
}
- List封装
Spring MVC 不支持 List 类型的直接转换,需要对 List 集合进行包装。
集合封装类
package com.southwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class UserList {
private List<User> users;
}
JSP写法,就是在name中加入数组的编号
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2019-03-14
Time: 09:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/data/list" method="post">
用户1编号:<input type="text" name="users[0].id"/><br/>
用户1名称:<input type="text" name="users[0].name"/><br/>
用户2编号:<input type="text" name="users[1].id"/><br/>
用户2名称:<input type="text" name="users[1].name"/><br/>
用户3编号:<input type="text" name="users[2].id"/><br/>
用户3名称:<input type="text" name="users[2].name"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
业务方法
@RequestMapping("/list")
public String list(UserList userList){
StringBuffer str = new StringBuffer();
for(User user:userList.getUsers()){
str.append(user);
}
return str.toString();
}
- Map封装
自定义封装类
package com.southwind.entity;
import lombok.Data;
import java.util.Map;
@Data
public class UserMap {
private Map<String,User> users;
}
业务方法
@RequestMapping("/map")
public String map(UserMap userMap){
StringBuffer str = new StringBuffer();
for(String key:userMap.getUsers().keySet()){
User user = userMap.getUsers().get(key);
str.append(user);
}
return str.toString();
}
JSP
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2019-03-14
Time: 09:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/data/map" method="post">
用户1编号:<input type="text" name="users['a'].id"/><br/>
用户1名称:<input type="text" name="users['a'].name"/><br/>
用户2编号:<input type="text" name="users['b'].id"/><br/>
用户2名称:<input type="text" name="users['b'].name"/><br/>
用户3编号:<input type="text" name="users['c'].id"/><br/>
用户3名称:<input type="text" name="users['c'].name"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
Spring MVC 模型数据解析(控制器数据传到页面)
Handler传到页面
JSP 四大作用域对应的内置对象:pageContext、request、session、application。
模型数据的绑定是由 ViewResolver 来完成的,实际开发中,我们需要先添加模型数据,再交给 ViewResolver 来绑定。
Spring MVC 提供了以下几种方式添加模型数据:
- Map
- Model
- ModelAndView
- @SessionAttribute
- @ModelAttribute
将模式数据绑定到 request 对象。
1、Map
@RequestMapping("/map")
public String map(Map<String,User> map){
User user = new User();
user.setId(1L);
user.setName("张三");
map.put("user",user);
return "view";
}
JSP
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2019-03-14
Time: 11:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.user}
${sessionScope.user}
${applicationScope.user}
</body>
</html>
2、Model
@RequestMapping("/model")
public String model(Model model){
User user = new User();
user.setId(1L);
user.setName("张三");
model.addAttribute("user",user);
return "view";
}
3、ModelAndView
Jsp写法
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>title</title>
</head>
<body>
<form>
学生ID:<input type="text" name="id" value="${student.id}"><br>
学生姓名:<input type="text" name="id" value="${student.name}"><br>
学生年龄:<input type="text" name="id" value="${student.age}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
绑定结果
// 这个用的比较多,其实记住一个即可
@RequestMapping("/modelAndView3")
public ModelAndView modelAndView3(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView("view");
modelAndView.addObject("user",user);
return modelAndView;
}
// 主要记住这个即可
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("view");
return modelAndView;
}
@RequestMapping("/modelAndView2")
public ModelAndView modelAndView2(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
View view = new InternalResourceView("/view.jsp");
modelAndView.setView(view);
return modelAndView;
}
@RequestMapping("/modelAndView4")
public ModelAndView modelAndView4(){
User user = new User();
user.setId(1L);
user.setName("张三");
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view);
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/modelAndView5")
public ModelAndView modelAndView5(){
User user = new User();
user.setId(1L);
user.setName("张三");
Map<String,User> map = new HashMap<>();
map.put("user",user);
ModelAndView modelAndView = new ModelAndView("view",map);
return modelAndView;
}
@RequestMapping("/modelAndView6")
public ModelAndView modelAndView6(){
User user = new User();
user.setId(1L);
user.setName("张三");
Map<String,User> map = new HashMap<>();
map.put("user",user);
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view,map);
return modelAndView;
}
@RequestMapping("/modelAndView7")
public ModelAndView modelAndView7(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView("view","user",user);
return modelAndView;
}
@RequestMapping("/modelAndView8")
public ModelAndView modelAndView8(){
User user = new User();
user.setId(1L);
user.setName("张三");
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view,"user",user);
return modelAndView;
}
4、HttpServletRequest
@RequestMapping("/request")
public String request(HttpServletRequest request){
User user = new User();
user.setId(1L);
user.setName("张三");
request.setAttribute("user",user);
return "view";
}
5、@ModelAttribute
- 定义一个方法,该方法专门用来返回要填充到模型数据中的对象。
@ModelAttribute
public User getUser(){
User user = new User();
user.setId(1L);
user.setName("张三");
return user;
}
@ModelAttribute
public void getUser(Map<String,User> map){
User user = new User();
user.setId(1L);
user.setName("张三");
map.put("user",user);
}
@ModelAttribute
public void getUser(Model model){
User user = new User();
user.setId(1L);
user.setName("张三");
model.addAttribute("user",user);
}
- 业务方法中无需再处理模型数据,只需返回视图即可。
@RequestMapping("/modelAttribute")
public String modelAttribute(){
return "view";
}
将模型数据绑定到 session 对象
1、直接使用原生的 Servlet API。
@RequestMapping("/session")
public String session(HttpServletRequest request){
HttpSession session = request.getSession();
User user = new User();
user.setId(1L);
user.setName("张三");
session.setAttribute("user",user);
return "view";
}
@RequestMapping("/session2")
public String session2(HttpSession session){
User user = new User();
user.setId(1L);
user.setName("张三");
session.setAttribute("user",user);
return "view";
}
2、@SessionAttribute
@SessionAttributes(value = {"user","address"})
public class ViewHandler {
}
对于 ViewHandler 中的所有业务方法,只要向 request 中添加了 key = “user”、key = “address” 的对象时,Spring MVC 会自动将该数据添加到 session 中,保存 key 不变。
@SessionAttributes(types = {User.class,Address.class})
public class ViewHandler {
}
对于 ViewHandler 中的所有业务方法,只要向 request 中添加了数据类型是 User 、Address 的对象时,Spring MVC 会自动将该数据添加到 session 中,保存 key 不变。
将模型数据绑定到 application 对象
@RequestMapping("/application")
public String application(HttpServletRequest request){
ServletContext application = request.getServletContext();
User user = new User();
user.setId(1L);
user.setName("张三");
application.setAttribute("user",user);
return "view";
}
Spring MVC REST
REST:Representational State Transfer,资源表现层状态转换,是目前比较主流的一种互联网软件架构,它结构清晰、标准规范、易于理解、便于扩展。
- 资源(Resource)
网络上的一个实体,或者说网络中存在的一个具体信息,一段文本、一张图片、一首歌曲、一段视频等等,总之就是一个具体的存在。可以用一个 URI(统一资源定位符)指向它,每个资源都有对应的一个特定的 URI,要获取该资源时,只需要访问对应的 URI 即可。
- 表现层(Representation)
资源具体呈现出来的形式,比如文本可以用 txt 格式表示,也可以用 HTML、XML、JSON等格式来表示。
- 状态转换(State Transfer)
客户端如果希望操作服务器中的某个资源,就需要通过某种方式让服务端发生状态转换,而这种转换是建立在表现层之上的,所有叫做"表现层状态转换"。
特点
- URL 更加简洁。
- 有利于不同系统之间的资源共享,只需要遵守一定的规范,不需要进行其他配置即可实现资源共享。
如何使用
REST 具体操作就是 HTTP 协议中四个表示操作方式的动词分别对应 CRUD 基本操作。
GET 用来表示获取资源。
POST 用来表示新建资源。
PUT 用来表示修改资源。
DELETE 用来表示删除资源。
Handler
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.entity.User;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
@RestController
@RequestMapping("/rest")
public class RESTHandeler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection<Student> findAll(HttpServletResponse response){
response.setContentType("text/json;charset=UTF-8");
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
studentRepository.deleteById(id);
}
}
StudentRepository
package com.southwind.repository;
import com.southwind.entity.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
StudentRepositoryImpl
package com.southwind.repository.impl;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Long,Student> studentMap;
static{
studentMap = new HashMap<>();
studentMap.put(1L,new Student(1L,"张三",22));
studentMap.put(2L,new Student(2L,"李四",23));
studentMap.put(3L,new Student(3L,"王五",24));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
Spring MVC 文件上传下载
单文件上传
使用 MultipartFile 组件完成上传,SpringBoot 对这种方式进行了封装。
- JSP
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2019-03-15
Time: 09:09
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<form action='/file/upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file'><br>
<button type='submit'>上传</button>
</form>
</body>
</html>
1、input 的 type 设置为 file。
2、form 的 method 设置为 post(get 请求只能将文件名传给服务器)
3、from 的 enctype 设置为 multipart/form-data(如果不设置只能将文件名传给服务器)
springMVC封装了MultipartFile类,我们可以使用MultipartFile类对文件上传下载进行操作,非常方便,主要是file.transferTo()
的封装节约了非常多的时间
- Handler
@PostMapping("/upload")
public String upload1(@RequestParam("file") MultipartFile file,HttpServletRequest request){
System.out.println(file);
if (file.getSize() > 0){
// 获取file文件夹的绝对路径
String path = request.getServletContext().getRealPath("file");
String name = file.getOriginalFilename();
File save = new File(path,name);
try {
file.transferTo(save);
if (name.indexOf(".png") != -1 || name.indexOf(".jpg") != -1) {
request.setAttribute("path","file/"+name);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "upload";
}
这样,文件就被我们上传到本地服务器的/Users/apple/Desktop/Computer-Process-Organization
目录下
JSP
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>多文件上传</title>
</head>
<body>
<form method="post" action="/file/multiUpload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Handler
@PostMapping("/uploads")
public String uploads(MultipartFile[] imgs,HttpServletRequest request){
// 保存上传后的图片地址
List<String> files = new ArrayList<>();
for (MultipartFile img:imgs){
if(img.getSize()>0){
//从Request获取保存上传文件的file路径
String path = request.getServletContext().getRealPath("file");
//获取上传的文件名
String name = img.getOriginalFilename();
File file = new File(path,name);
try {
img.transferTo(file);
//保存上传之后的文件路径
files.add("/file/"+name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 添加到request中,以便jsp读取
request.setAttribute("files",files);
return "uploads";
}
下载
- JSP
使用Jsp给一个get请求,然后使用这个get请求来下载
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/file/download/1">1.png</a>
<a href="/file/download/2">2.png</a>
<a href="/file/download/3">3.png</a>
</body>
</html>
- Handler
@GetMapping("/download/{name}")
public void download(@PathVariable("name") String name, HttpServletRequest request, HttpServletResponse response){
if(name != null){
name += ".png";
String path = request.getServletContext().getRealPath("file");
File file = new File(path,name);
OutputStream outputStream = null;
if(file.exists()){
response.setContentType("application/forc-download");
response.setHeader("Content-Disposition","attachment;filename="+name);
try {
outputStream = response.getOutputStream();
outputStream.write(FileUtils.readFileToByteArray(file));
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
注意:这里使用FileUtils
类需要导入相关依赖,这个文件转比特流特别好用,可以多用一下
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
Spring MVC 表单标签库
可以先将一个类添加到ModelAndView中,然后再
- Handler
@GetMapping("/get")
public ModelAndView get(){
ModelAndView modelAndView = new ModelAndView("tag");
Student student = new Student(1L,"张三",22);
modelAndView.addObject("student",student);
return modelAndView;
}
- JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<form:form modelAttribute="student">
学生ID:<form:input path="id"/><br/>
学生姓名:<form:input path="name"/><br/>
学生年龄:<form:input path="age"/><br/>
<input type="submit" value="提交"/>
</form:form>
</body>
</html>
1、JSP 页面导入 Spring MVC 表单标签库,与导入 JSTL 标签库的语法非常相似,前缀 prefix 可以自定义,通常定义为 form。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
2、将 form 表单与模型数据进行绑定,通过 modelAttribute 属性完成绑定,将 modelAttribute 的值设置为模型数据对应的 key 值。
Handeler:modelAndView.addObject("student",student);
JSP:<form:form modelAttribute="student">
3、form 表单完成绑定之后,将模型数据的值取出绑定到不同的标签中,通过设置标签的 path 属性完成,将 path 属性的值设置为模型数据对应的属性名即可。
学生ID:<form:input path="id"/><br/>
学生姓名:<form:input path="name"/><br/>
学生年龄:<form:input path="age"/><br/>
常用的表单标签
- from
<form:form modelAttribute="student"/>
渲染的是 HTML 中的<form></form>
,通过 modelAttribute 属性绑定具体的模型数据。
- input
<form:input path="name"/>
渲染的是 HTML 中的 <input type="text"/>
,from 标签绑定的是模型数据,input 标签绑定的是模型数据中的属性值,通过 path 属性可以与模型数据中的属性名对应,并且支持及联操作。
<from:input path="address.name"/>
- password
<form:password path="password"/>
渲染的是 HTML 中的 <input type="password"/>
,通过 path 属性与模型数据的属性值进行绑定,password 标签的值不会在页面显示。
- checkbox
<form:checkbox path="hobby" value="读书"/>
student.setFlag(false);
checkbox:<form:checkbox path="flag" value="flag"></form:checkbox><br/>
渲染的是 HTML 中的 <input type="checkbox"/>
,通过 path 与模型数据的属性值进行绑定,可以绑定 boolean、数组和集合。
如果绑定 boolean 值,若该变量的值为 true,则表示该复选框选中,否则表示不选中。
如果绑定数组或者集合,数组/集合中的元素等于 checkbox 的 value 值,则选中。
student.setHobby(Arrays.asList("读书","看电影","玩游戏"));
modelAndView.addObject("student",student);
爱好:<form:checkbox path="hobby" value="摄影"></form:checkbox>摄影<br/>
<form:checkbox path="hobby" value="读书"></form:checkbox>读书<br/>
<form:checkbox path="hobby" value="听音乐"></form:checkbox>听音乐<br/>
<form:checkbox path="hobby" value="看电影"></form:checkbox>看电影<br/>
<form:checkbox path="hobby" value="旅游"></form:checkbox>旅游<br/>
<form:checkbox path="hobby" value="玩游戏"></form:checkbox>玩游戏<br/>
<input type="submit" value="提交"/>
- checkboxes
<form:checkboxes items=${student.hobby} path="selecHobby"/>
渲染的是 HTML 中的一组 <input type="checkbox"/>
,是对 <form:checkbox/>
的一种简化,需要结合 items 和 path 属性来使用,items 绑定被遍历的集合或数组,path 绑定被选中的集合或数组,可以这样理解,items 为全部可选集合,path 为默认的选中集合。
student.setHobby(Arrays.asList("摄影","读书","听音乐","看电影","旅游","玩游戏"));
student.setSelectHobby(Arrays.asList("摄影","读书","听音乐"));
modelAndView.addObject("student",student);
爱好:<form:checkboxes path="selectHobby" items="${student.hobby}"/><br/>
需要注意的是 path 可以直接绑定模型数据的属性值,items 则需要通过 EL 表达式的形式从域对象中获取数据,不能直接写属性名。
- rabiobutton
<from:radiobutton path="radioId" value="0"/>
渲染的是 HTML 中的一个 <input type="radio"/>
,绑定的数据与标签的 value 值相等则为选中,否则不选中。
student.setRadioId(1);
modelAndView.addObject("student",student);
radiobutton:<form:radiobutton path="radioId" value="1"/>radiobutton<br/>
- radiobuttons
<form:radiobuttons itmes="${student.grade}" path="selectGrade"/>
渲染的是 HTML 中的一组 <input type="radio"/>
,这里需要结合 items 和 path 两个属性来使用,items 绑定被遍历的集合或数组,path 绑定被选中的值,items 为全部的可选类型,path 为默认选中的选项,用法与 <form:checkboxes/>
一致。
Map<Integer,String> gradeMap = new HashMap<>();
gradeMap.put(1,"一年级");
gradeMap.put(2,"二年级");
gradeMap.put(3,"三年级");
gradeMap.put(4,"四年级");
gradeMap.put(5,"五年级");
gradeMap.put(6,"六年级");
student.setGradeMap(gradeMap);
student.setSelectGrade(3);
modelAndView.addObject("student",student);
学生年级:<form:radiobuttons items="${student.gradeMap}" path="selectGrade"/><br/>
- select
<form:select items="${student.citys}" path="selectCity"/>
渲染的是 HTML 中的一个 <select/>
标签,需要结合 items 和 path 两个属性来使用,items 绑定被遍历的集合或数组,path 绑定被选中的值,用法与 <from:radiobuttons/>
一致。
Map<Integer,String> cityMap = new HashMap<>();
cityMap.put(1,"北京");
cityMap.put(2,"上海");
cityMap.put(3,"广州");
cityMap.put(4,"深圳");
student.setCityMap(cityMap);
student.setSelectCity(3);
modelAndView.addObject("student",student);
所在城市:<form:select items="${student.cityMap}" path="selectCity"></form:select><br/>
- options
form:select
结合 form:options
的使用,from:select
只定义 path 属性,在 form:select
标签内部添加一个子标签 form:options
,设置 items 属性,获取被遍历的集合。
所在城市:<form:select path="selectCity">
<form:options items="${student.cityMap}"></form:options>
</form:select><br/>
-
option
form:select
结合form:option
的使用,from:select
定义 path 属性,给每一个form:option
设置 value 值,path 的值与哪个 value 值相等,该项默认选中。
所在城市:<form:select path="selectCity">
<form:option value="1">杭州</form:option>
<form:option value="2">成都</form:option>
<form:option value="3">西安</form:option>
</form:select><br/>
- textarea
渲染的是 HTML 中的一个 <textarea/>
,path 绑定模型数据的属性值,作为文本输入域的默认值。
student.setIntroduce("你好,我是...");
modelAndView.addObject("student",student);
信息:<form:textarea path="introduce"/><br/>
- errors
处理错误信息,一般用在数据校验,该标签需要结合 Spring MVC 的验证器结合起来使用。
Spring MVC 数据校验
Spring MVC 提供了数据校验的方式:使用 Annotation JSR - 303 标准进行校验。
使用 Annotation JSR - 303 标准不需要自定义验证器,通过注解的方式可以直接在实体类中添加每个属性的验证规则,这种方式更加方便,实际开发中推荐使用。
Annotation JSR - 303 标准
使用 Annotation JSR - 303 标准进行验证,需要导入支持这种标准的依赖 jar 文件,这里我们使用 Hibernate Validator。
- pom.xml
<!-- JSR-303 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
- 通过注解的方式直接在实体类中添加相关的验证规则。
package com.southwind.entity;
import lombok.Data;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
@Data
@Validated
public class Person {
@NotEmpty(message = "用户名不能为空")
private String username;
@Size(min = 6,max = 12,message = "密码6-12位")
private String password;
@Email(regexp = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\\\.[a-zA-Z0-9-]+)*\\\\.[a-zA-Z0-9]{2,6}$",message = "请输入正确的邮箱格式")
private String email;
@Pattern(regexp = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\\\\\\\d{8}$",message = "请输入正确的电话")
private String phone;
}
- ValidatorHandler
@GetMapping("/register")
public String register(Model model){
model.addAttribute("person",new Person());
return "register";
}
@PostMapping("/register")
public String register(@Valid Person person, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "register";
}
return "index";
}
- JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form:form modelAttribute="person" action="/validator/register2" method="post">
用户名:<form:input path="username"></form:input><form:errors path="username"/><br/>
密码:<form:password path="password"></form:password><form:errors path="password"/><br/>
邮箱:<form:input path="email"></form:input><form:errors path="email"/><br/>
电话:<form:input path="phone"></form:input><form:errors path="phone"/><br/>
<input type="submit" value="提交"/>
</form:form>
</body>
</html>
校验规则详解:
@Null
被注解的元素必须为null
@NotNul
被注解的元素不能为null
@Min(value)
被注解的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value)
被注解的元素必须是一个数字,其值必须小于于等于指定的最大值
@Email
被注解的元素必须是电子邮箱地址
@Pattern
被注解的元素必须符合对应的正则表达式
@Length
被注解的元素的大小必须在指定的范围内
@NotEmpty
被注解的字符串的值必须非空
Null 和 Empty 是不同的结果,String str = null
,str 是 null,String str = ""
,str 不是 null,其值为空。