问题描述
看了 rest风格的增删改查 视屏后,我照着视屏中的内容实现rest风格的接口,但是雷丰阳在视屏里,表单中调用完接口后是能正确转发到其他界面的,但是我就一直是405,我的接口中的语句输出了,也就是接口访问到了,但是网页跳转不了,get/post/put/delete 四种请求方式只有get能正确跳转网页(四种返回的都是一样的地址),请问怎么解决呢??
相关代码web.xml<?xml version="1.0" encoding="UTF-8"?>
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">
index.html
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc-config.xml
1
springDispatcherServlet
/
default
/index.html
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
springmvc-config.xml<?xml version="1.0" encoding="UTF-8"?>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
RestController.javapackage controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author Surrin1999
*/
@Controller
@RequestMapping("/part1")
public class RestController {
@RequestMapping(value = "/book/{id}", method = RequestMethod.POST)
public String addBook(@PathVariable String id) {
System.out.println("正在增加图书..." + "id:" + id);
return "/success.html";
}
@RequestMapping(value = "/book/{id}", method = RequestMethod.DELETE)
public String deleteBook(@PathVariable String id) {
System.out.println("正在删除图书..." + "id:" + id);
return "/success.html";
}
@RequestMapping(value = "/book/{id}/{name}", method = RequestMethod.PUT)
public String updateBook(@PathVariable String id, @PathVariable String name) {
System.out.println("正在更新图书..." + "id:" + id + ",name:" + name);
return "/success.html";
}
@RequestMapping(value = "/book/{id}", method = RequestMethod.GET)
public String selectBook(@PathVariable String id) {
System.out.println("正在查找图书..." + "id:" + id);
return "/success.html";
}
}form.html
Title包结构图
测试图片
上图中的警告信息如下20-May-2021 22:31:31.632 警告 [http-nio-8080-exec-5] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.logException Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported]
2021-5-21 更新