1. 程式人生 > >springmvc get post put delete

springmvc get post put delete

ping font tput delet input att str req stp

web。xml

<!-- 
    配置 org.springframework.web.filter.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>
/**
     * 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("testRest Put: " + id); return SUCCESS; } @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id) { System.out.println(
"testRest Delete: " + id); return SUCCESS; } @RequestMapping(value = "/testRest", method = RequestMethod.POST) public String testRest() { System.out.println("testRest POST"); return SUCCESS; } @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET) public String testRest(@PathVariable Integer id) { System.out.println("testRest GET: " + id); return SUCCESS; }
<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 Rest Get</a>

springmvc get post put delete