SpringMVC之RequestMethod

帮助文档中说明:

Fortunately, there are two possible workarounds: you can either use JavaScript to do your PUT or DELETE, or simply do a POST with the 'real' method as an additional parameter (modeled as a hidden input field in an HTML form). This latter trick is what Spring’s HiddenHttpMethodFilter does. This filter is a plain Servlet Filter and therefore it can be used in combination with any web framework (not just Spring MVC). Simply add this filter to your web.xml, and a POST with a hidden _method parameter will be converted into the corresponding HTTP method request.

这里,总结出4点:

1⃣️表单中method="post"

2⃣️在HTML表单中添加隐藏域(这里测试使用,就不使用隐藏域了),

3⃣️在web.xml中配置HiddenHttpMethodFilter过滤器

4⃣️隐藏域name="_method"


1、配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloSpringMVC</display-name>
  <welcome-file-list>
    <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> -->
  	<servlet-name>hello</servlet-name>
  </filter-mapping>
  
  <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <!-- 所有资源的访问必须要经过DispatcherServlet处理 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2、核心配置文件:

<?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-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
	<!-- 启动SpringMVC的注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 组件扫描器 支持ant风格 -->
	<context:component-scan base-package="com.**.controller"></context:component-scan>
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>
3、新建jsp文件(方便测试):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>get请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="get">
	<input type="submit" value="请求">
</form>
<hr/>
<h2>post请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="post">
	<input type="submit" value="请求">
</form>
<hr/>
<h2>put请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="put">
	<input type="submit" value="请求">
</form>
<hr/>
<h2>delete请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="delete">
	<input type="submit" value="请求">
</form>
<hr/>
<h2>head请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="head">
	<input type="submit" value="请求">
</form>
<hr/>
<h2>OPTIONS请求</h2>
<form action="requestMethod" method="post">
	<input type="text" name="_method" value="OPTIONS">
	<input type="submit" value="请求">
</form>
<hr/>
</body>
</html>
4、Controller类:

package com.slz.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MethodController {
	@RequestMapping(path="requestMethod",method=RequestMethod.GET)
	public ModelAndView testMethod01(){
		System.out.println("getMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.POST)
	public ModelAndView testMethod02(){
		System.out.println("postMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.PUT)
	public ModelAndView testMethod03(){
		System.out.println("putMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.DELETE)
	public ModelAndView testMethod04(){
		System.out.println("deleteMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.HEAD)
	public ModelAndView testMethod05(){
		System.out.println("headMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.OPTIONS)
	public ModelAndView testMethod06(){
		System.out.println("optionsMethod");
		return null;
	}
	@RequestMapping(path="patchMethod",method=RequestMethod.PATCH)
	public ModelAndView testMethod07(){//Request method 'PATCH' not supported
		System.out.println("patchMethod");
		return null;
	}
	@RequestMapping(path="requestMethod",method=RequestMethod.TRACE)
	public ModelAndView testMethod08(){//HTTP Status 405 - Request method 'TRAC' not supported
		System.out.println("traceMethod");
		return null;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值