springmvc of HiddenHttpMethodFilter configuration using REST-style URL

REST: That resource presentation layer state transformation.

Resources: An entity on the network. Each resource corresponds to a specific URL.

Presentation Layer: to show up in the form of specific resources, such as text represented as txt, html, xml, json or binary.

State transformation: each send a request on behalf of an interactive process of client and server, and HTTP is a stateless protocol request that all states are stored on the server side. Therefore, if the client wants to operate on the server side, must by some means. And this transformation is based on the presentation layer, presentation layer so that the state of transformation. Specifically, the HTTP protocol is the verb indicates the operation mode of the four: GET, POST, PUT, DELETE

To CURD for example, REST-style URL:

New: / order Post  

Modify: / order / 1 Put ago: update id = 1?

Delete: / order / 1 Delete previous: selete id = 1?

Gets: / order / 1 Get ago: get id = 1?

How to send PUT and DELETE requests it?

(1) needs to be configured in web.xml HiddenHttpMethodFilter.

(2) needs to send a POST request.

(3) needs to send name = "_ method" POST request when sending hidden fields, or DELETE value PUT.

How to get past the argument made in the target method springmvc, such as id it?

Use @PathVariable comment.

In web.xml:

    <!-- 
    配置 org.springframework.web.filter.HiddenHttpMethodFilter 
    -->
    <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>

SpringmvcTest.java

package com.gong.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";
    
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
    public String testGet(@PathVariable("id") Integer id) {
        System.out.println("get-->"+id);
        return SUCCESS;
    }
    @RequestMapping(value="/testRest",method=RequestMethod.POST)
    public String testPost() {
        System.out.println("post-->");
        return SUCCESS;
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
    public String testPut(@PathVariable("id") Integer id) {
        System.out.println("put-->"+id);
        return SUCCESS;
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
    public String testDelete(@PathVariable("id") Integer id) {
        System.out.println("delete-->"+id);
        return SUCCESS;
    }
}

index.jsp

    <a href="springmvc/testRest/1">Get</a>
    <br><br>
    <form action="springmvc/testRest" method="POST">
        <input type="submit" value="submit">
    </form>
    <br><br>
        <form action="springmvc/testRest/1" method="POST">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="delete">
    </form>
    <br><br>
    <form action="springmvc/testRest/1" method="POST">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="put">
    </form>
    <br><br>

After starting ttomcat server:

 

Click on the appropriate submission:

In the console output:

 

Description of the request is to call a different way.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12177670.html