• SpringMVC----REST+HiddenHttpMethodFilter


    1.概述

      1.1  REST:即Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。

          资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。

                      每种资源对应一个特定的URL。因此,URL也是每一个资源的独一无二的识别符。

          表现层(Representation):把资源具体呈现出来的形式,叫做他的表现层;

          状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。

                        因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”。而这种转化是建立在表现层之上的,

                          所以就是“表现层状态转化”。具体说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。

                        他们分别对应四种基本操作:GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源。

    2.示例

      2.1  /order/1 HTTP GET:得到id=1的order;

          /order/1 HTTP DELETE:删除id=1的order;

          /order/1 HTTP PUT:更新id=1的order;

          /order    HTTP POST:新增order;

    3.HiddenHttpMethodFilter:

         浏览器form表单只支持GET和POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。

    4.测试代码

    先配置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"
        version="3.0">
        <display-name>SPRING-MVC-01</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>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <!-- 配置DispatcherServlet -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置DispatcherServlet的一个初始化参数 -->
            <!-- 作用:配置springMVC配置文件的位置和名称 -->
            <!-- <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param> -->
            <!-- 实际上也可以不通过 contextConfigLocation来配置springMVC的配置文件,而使用默认的
                默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml
            -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter -->
        <!-- 作用:可以吧POST请求转为DELETE或PUT请求 -->
        <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>
        
    </web-app>
    
    
    
    package com.yk.springmvc.handlers;
    
    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;
    
    @RequestMapping("/springmvc")
    @Controller
    public class SpringMVCTest {
        
        private static final String SUCCESS = "success";
        
        /**
         * Rest风格的URL.
         * 以CRUD为例:
         * 新增:/order POST        <一样。>
         * 修改:/order/1 PUT        <update?id=1>
         * 获取:/order/1 GET        <get?id=1>
         * 删除:/order/1 DELETE    <delete?id=1>
         * 
         * 如何发送PUT请求、DELETE请求?
         * 1.需要配置HiddenHttpMethodFilter;
         * 2.需要发送POST请求;
         * 3.需要在发送POST请求时携带一个隐藏域name="_method"的隐藏域,值为DELETE/PUT;
         * 
         * 在springMVC中的目标方法中,如何得到id呢?
         *         使用@PathVariable注解
         */
        @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
        public String testRestPut(@PathVariable Integer id){
            System.out.println("SpringMVCTest.testRestPut(PUT请求)"+id);
            return SUCCESS;
        }
        
        @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
        public String testRestDelete(@PathVariable Integer id){
            System.out.println("SpringMVCTest.testRestDelete(DELETE请求)"+id);
            return SUCCESS;
        }
        
        @RequestMapping(value="/testRest",method=RequestMethod.POST)
        public String testRest(){
            System.out.println("SpringMVCTest.testRest(POST请求)");
            return SUCCESS;
        }
        
        @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
        public String testRest(@PathVariable Integer id){
            System.out.println("SpringMVCTest.testRest(GET请求)"+id);
            return SUCCESS;
        }
        
        
        @RequestMapping("/testRequestMapping")
        public String testRequestMapping(){
            System.out.println("SpringMVCTest.testRequestMapping()");
    //        return "success";    下面会有很多,所以定义一个常量
            return SUCCESS;
        }    
    }
    
    
    index.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>
        <form action="springmvc/testRest/1" method="POST">
            <input type="hidden" name="_method" value="PUT" /> 
            <input type="submit" value="TestRest_PUT" />
        </form>
        <br />
        <br />
        <form action="springmvc/testRest/1" method="POST">
            <input type="hidden" name="_method" value="DELETE" /> 
            <input type="submit" value="TestRest_DELETE" />
        </form>
        <br />
        <br />
        <form action="springmvc/testRest" method="POST">
            <input type="submit" value="TestRest_POST" />
        </form>
        <br />
        <br />
        <a href="springmvc/testRest/1">Test GET</a>
        <br />
        <br />
        <a href="springmvc/testRequestMapping">Test--RequestMapping</a>
        <br />
        <br />
        <a href="helloworld">Hello World</a>
    </body>
    </html>

    5.控制台显示结果

    SpringMVCTest.testRest(GET请求)1
    SpringMVCTest.testRest(POST请求)
    SpringMVCTest.testRestDelete(DELETE请求)1
    SpringMVCTest.testRestPut(PUT请求)1
  • 相关阅读:
    牛客网PAT练习场-有几个PAT
    牛客网PAT练习场-到底买不买
    增量数据捕获cdc
    windows terminal 笔记
    ubuntu文件夹颜色设置及vim颜色配置
    windows sub system 如何修改root密码
    Intellij IDEA 一个Tomcat启动多个Web的设置
    What is “Deploy applications configured in Tomcat instance” in IntelliJidea
    接口批量测试
    使用soupUI,jemter 测试http接口的方法
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9727235.html
Copyright © 2020-2023  润新知