Rest-优雅的请求风格(图书增删改查的案例)
前的浏览器只支持 post/get 请求,因此为了得到 put/delete 的请求方式需要使用 Spring
提供的 HiddenHttpMethodFilter 过滤器进行转换(只能转换post).
前端代码
<%--
Created by IntelliJ IDEA.
User: YRX
Date: 2024/3/13
Time: 13:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript" src="script/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
$(function () {
// alert("asdfg")
$("#deleteBook").click(function (){
$("#hiddenBook").attr("action",this.href);
//alert("asfaf")
$(":hidden").val("DELETE");
$("#hiddenBook").submit();
return false;
});
})
</script>
<body>
<%--查询图书--%>
<h1>查询指定id图书</h1>
<a href="user/book/600">查询图书</a><br>
<h1>Rest添加图书</h1><br>
<form action="user/book" method="post">
<input type="text" name="bookName">
<input type="submit">
</form>
<h1>删除指定id的图书</h1>
<a href="user/book/600" id="deleteBook" >删除指定id的书</a><br>
<form action="" method="post" id="hiddenBook">
<input type="hidden" name="_method">
</form>
<h1>修改图书图书</h1>
<form action="user/book/700" method="post">
<input type="hidden" name="_method" value="PUT">
<input TYPE="submit" value="提交">
</form>
</body>
</html>
后端代码
package com.ysbt.Rest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/user")
public class RestHandle {
//查询指定图书
@GetMapping("/book/{id}")
public String selectBook(@PathVariable("id") String id){
System.out.println("查询到"+id+"的书");
return "success";
}
//添加图书
@PostMapping("/book")
public String addBook(String bookName){
System.out.println("bookName="+bookName);
return "success";
}
//删除图书
@RequestMapping(value = "/book/{id}" ,method = RequestMethod.DELETE)
public String deleteBook(@PathVariable("id") String id){
System.out.println("删除"+id+"的图书");
return "redirect:/user/success";
}
//修改图书
@PutMapping("book/{id}")
public String updateBook(@PathVariable("id") String id){
System.out.println("修改的图书的id为"+"id");
return "redirect:/user/success";
}
@RequestMapping("/success")
public String success(){
return "success";
}
}
springDispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.ysbt.*"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--当时就是因为没有配置这两个,无法识别jquery,-->
<!-- 能够支持springmvc的高级功能,比如JSR303校验,支持映射动态请求-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 将springmvc不能处理的请求,交给tomcat处理,比如css,js-->
<mvc:default-servlet-handler/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置过滤器,不配置的话就无法把post请求转换别的请求-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置前端控制器、中央控制器、分发控制器,用户的请求都会经过它的处理-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置属性contextConfigLocation,指定DispatcherServlet去操作的spring配置文件-->
<!-- 项目启动时自动加载DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>