1. 程式人生 > >SpringMVC rest風格傳送請求 及@PathVariable

SpringMVC rest風格傳送請求 及@PathVariable

REST:Representational  State  Transfer

用URL定位資源,用HTTP做描述操作  

使用post     delete    put    get  分別對應CRUD操作      spring3.0開始支援rest風格的操作

使用@PathVariable接受引數

 

 

如何傳送put  delete請求

預設情況下,form表單是不支援傳送put和delete請求  只支援post和get請求

可以通過配置過濾器將post請求轉換為put或delete請求

 

配置過濾器

 <!--配置隱藏方法過濾器-->
<filter>
    <filter-name>hiddenMethod</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
    <filter-mapping>
        <filter-name>hiddenMethod</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

在表單中寫入隱藏方法

 <form action="${pageContext.request.contextPath}/testrestform222/100" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="測試aa">
    </form>

注意:type必須寫為hidden    name必須為  _method      value為put或delete

 @RequestMapping(value = "/testrestform222/{id}",method =RequestMethod.PUT)
    public String testrest(@PathVariable Integer id){
        System.out.println(id);
        return "/second.jsp";
    }

從tomcat8開始,JSPs only permit GET POST or HEAD

即jsp只能識別  get  post  或head請求,因此不能使用普通的請求轉發(會報錯)   要使用重定向(redirect)