SprinMVC HiddenHttpMethodFilter 过滤器

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88797697

Rest简介

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前 最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用

• 资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它 可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。 可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要 获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识 别符。

• 表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层 (Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格 式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。

• 状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一 次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器 端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“ 状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “ 表现层状态转化” 。具体说,就是 HTTP 协议里面,四个表示操作方式的动 词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获 取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。

注意:若要使用DELETE和PUT请求方法,必须在类中添加@ResponseBody注解,否则将会报JSPs only permit GET POST or HEAD错误。具体操作看HelloWorld.class类。

web.xml配置

    <!--配置 HiddenHttpMethodFilter :可以把POST请求转为DELETE 或 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>

index.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18: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>
  <body>
    <form action="helloworld_mvc/testRest1/1001" method="post">
      <input type="hidden" name="_method" value="GET">
      <input type="submit" value="GET请求">
    </form>
    <br>
    <br>
    <form action="helloworld_mvc/testRest1/1001" method="post">
      <input type="hidden" name="_method" value="POST">
      <input type="submit" value="POST请求">
    </form>
    <br>
    <br>
    <form action="helloworld_mvc/testRest1/1001" method="post">
      <input type="hidden" name="_method" value="DELETE">
      <input type="submit" value="DELETE请求">
    </form>
    <br>
    <br>
    <form action="helloworld_mvc/testRest1/1001" method="post">
      <input type="hidden" name="_method" value="PUT">
      <input type="submit" value="PUT请求">
    </form>
    <br>
    <br>
  </body>
</html>

HelloWorld.class类

package com.helloworld;

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;
import org.springframework.web.filter.HiddenHttpMethodFilter;

import javax.swing.text.html.HTMLDocument;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping(value = "testRest1/{id}", method = RequestMethod.GET)
    public String testRest1(@PathVariable Integer id){
        return "success";
    }
    @RequestMapping(value = "testRest1/{id}", method = RequestMethod.POST)
    public String testRest2(@PathVariable Integer id){
        return "success";
    }
    @RequestMapping(value = "testRest1/{id}", method =  RequestMethod.DELETE)
    @ResponseBody
    public String testRest3(@PathVariable Integer id){
        return "success";
    }
    @RequestMapping(value = "testRest1/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public String testRest4(@PathVariable Integer id){
        return "success";
    }


}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88797697