传统的 Web 开发中,from 只⽀持 GET 和 POST,不⽀持 DELETE 和 PUT,如何解决?通过添加HiddenHttpMethodFilter 过滤器,可以将 POST 请求转为 PUT 或者 DELETE。
HiddenHttpMethodFilter 的实现原理
HiddenHttpMethodFilter 检测请求参数重是否包含 _method 参数,如果包含则取出它的值,并且判断请求类型之后完成请求类型的转换,然后继续传递。
实现步骤
- 在 form 表单中添加隐藏域标签,name 为 _method,value 为 DELETE/PUT。
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-11
Time: 11:53
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="/rest/update" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
- web.xml 中配置 HiddenHttpMethodFilter。
<!-- HiddenHttpMethodFilter -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filterclass>
</filter> <filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Handler
@PutMapping("/update")
@ResponseBody
public String update(HttpServletResponse response){
response.setCharacterEncoding("UTF-8");
return "已接收到PUT请求"; }
@DeleteMapping("/delete")
@ResponseBody
public String delete(HttpServletResponse response){
response.setCharacterEncoding("UTF-8");
return "已接收到DELETE请求"; }
需求分析
- ⭐添加课程,成功则返回全部课程信息。
- ⭐查询课程,通过 id 查询对应课程信息。
- ⭐修改课程,成功则返回修改之后的全部课程信息。
- ⭐删除课程,成功则返回删除之后的全部课程信息。
代码实现
- JSP
- ⭐添加课程:add.jsp
- ⭐修改课程:edit.jsp
- ⭐课程展示:index.jsp
- Corese实体类
public class Course{
private Integer id;
private String name;
private Double price; }
- CourseRepository
package com.southwind.repository;
import com.southwind.entity.Course;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class CourseRepository {
private Map<Integer, Course> courseMap;
public CourseRepository() {
courseMap = new HashMap<>();
courseMap.put(1,new Course(1,"Java基础",Double.parseDouble("500")));
courseMap.put(2,new Course(2,"Java⾼级",Double.parseDouble("600")));
courseMap.put(3,new Course(3,"企业级框架",Double.parseDouble("700")));
}
public Collection<Course> findAll(){
return courseMap.values();
}
public Course findById(Integer id){
return courseMap.get(id);
}
public void saveOrUpdate(Course course){
courseMap.put(course.getId(),course);
}
public void deleteById(Integer id){
courseMap.remove(id);
}
}
- CourseController
package com.southwind.controller;
import com.southwind.entity.Course;
import com.southwind.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseRepository courseRepository;
@PostMapping("/save")
public String save(Course course){
courseRepository.saveOrUpdate(course);
return "redirect:/course/findAll";
}
@GetMapping("/findAll")
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list",courseRepository.findAll());
return modelAndView;
}
@DeleteMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id") Integer id){
courseRepository.deleteById(id);
return "redirect:/course/findAll";
}
@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable("id") Integer id){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("edit");
modelAndView.addObject("courser",courseRepository.findById(id));
return modelAndView;
}
@PutMapping("/update")
public String update(Course course){
courseRepository.saveOrUpdate(course);
return "redirect:/course/findAll";
}
}
- JSP
sava.jsp
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-11
Time: 16:48
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="/course/save" method="post">
<table>
<tr>
<td>课程编号:</td>
<td>
<input type="text" name="id"/>
</td>
</tr>
<tr>
<td>课程名称:</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>课程价格:</td>
<td>
<input type="text" name="price"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交"/>
</td>
<td>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
index.jsp
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-11
Time: 16:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html> <head>
<title>Title</title>
</head> <body>
<table>
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="course">
<tr>
<td>${course.id}</td>
<td>${course.name}</td>
<td>${course.price}</td>
<td>
<form action="/course/deleteById/${course.id}"
method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="删除"/>
</form>
<a href="/course/findById/${course.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
edit.jsp
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-11
Time: 17:13
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>
<form action="/course/update" method="post">
<input type="hidden" name="_method" value="PUT"/>
<table>
<tr>
<td>编号:</td>
<td>
<input type="text" name="id" readonly
value="${courser.id}"/>
</td>
</tr>
<tr>
<td>名称:</td>
<td>
<input type="text" name="name" value="${courser.name}"/>
</td>
</tr>
<tr>
<td>价格:</td>
<td>
<input type="text" name="price" value="${courser.price}"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="修改"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Spring MVC实现⽂件的上传下载
文件上传
单⽂件上传
- 底层使⽤的是 Apache fileupload 组件完成上传功能,Spring MVC 只是对其进⾏了封装,简化开发,pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
- JSP页面
- ⭐input 的 type 设置为 file
- ⭐form 表单的 method 设置为 post
- ⭐form 表单的 enctype 设置为 multipart/form-data
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-11
Time: 17:28
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>
<form action="/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="img"/>
<input type="submit" value="提交"/>
</form>
<img src="${src}"/>
</body>
</html>
Handler
@PostMapping("/upload")
public String upload(@RequestParam("img") MultipartFile img,
HttpServletRequest request) {
if(img.getSize()>0){
String path =
request.getSession().getServletContext().getRealPath("file");
String fileName = img.getOriginalFilename();
File file = new File(path,fileName);
try {
img.transferTo(file);
request.setAttribute("src","/file/"+fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return "upload"; }
springmvc.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
多文件上传
JSP
<%--
Created by IntelliJ IDEA.
User: southwind
Date: 2020-02-12
Time: 11:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html> <head>
<title>Title</title>
</head> <body>
<form action="/file/uploads" method="post" enctype="multipart/form-data">
file1:<input type="file" name="imgs"/><br/>
file2:<input type="file" name="imgs"/><br/>
file3:<input type="file" name="imgs"/><br/>
<input type="submit" value="提交"/>
</form>
<c:forEach items="${list}" var="path">
<img width="300px" src="${path}">
</c:forEach>
</body>
</html>
Handler
@PostMapping("/uploads")
public String uploads(@RequestParam("imgs") MultipartFile[]
imgs,HttpServletRequest request){
List<String> pathList = new ArrayList<>();
for(MultipartFile img:imgs){
if(img.getSize()>0){
String path =
request.getSession().getServletContext().getRealPath("file");
String fileName = img.getOriginalFilename();
File file = new File(path,fileName);
try {
img.transferTo(file);
pathList.add("/file/"+fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
request.setAttribute("list",pathList);
return "uploads"; }