在控制台可以输出PUT、DELETE信息,但是跳转后页面405,所以在跳转后的jsp页面的头信息加配置isErrorPage="true"
index.jsp前端页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="test/id=2">test get</a>
<br>
<form action="test" method="post">
<input type="submit" value="test post">
</form>
<br>
<form action="test/id=3" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="test PUT">
</form>
<br>
<form action="test/id=4" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="test DELETE">
</form>
<br>
<a href="hello">hello world </a><br>
<a href="update/id=1">test path variable</a>
</body>
</html>
2、success.jsp前端页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>success page</h1>
</body>
</html>
3、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc_001</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--<param-value>/WEB-INF/springmvc-servlet.xml</param-value> -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、springmvc.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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.aihaokeji"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5、controller
package com.aihaokeji;
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;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Helloworld {
@RequestMapping(value = "/test/id={id}",method = RequestMethod.GET)
public String testget(@PathVariable("id") Integer id) {
System.out.println("get "+ id);
return"success";
}
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String testpost() {
System.out.println("post");
return"success";
}
@RequestMapping(value = "/test/id={id}",method = RequestMethod.PUT)
public String testput(@PathVariable("id") Integer id) {
System.out.println("put: "+ id);
return"success";
}
@RequestMapping(value = "/test/id={id}",method = RequestMethod.DELETE)
public String testdelete(@PathVariable("id") Integer id) {
System.out.println("delete: "+ id);
return"success";
}
@RequestMapping("/hello")
public String helloworld() {
System.out.println("success");
return"success";
}
@RequestMapping(value ="update/id={id}")
public String pathValiable(@PathVariable("id") Integer id) {
System.out.println("path valiable "+id);
return "success";
}
}