victory的博客

长安一片月,万户捣衣声

0%

SpringMVC | REST风格GetPostPutDelete请求

REST风格 GET/POST/PUT/DELETE请求

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

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

HiddenHttpMethodFilter支持REST风格的过滤器:可以将POST请求转换为PUT或DELETE请求

示例

1.项目目录

2.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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC02</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 支持REST风格的过滤器:可以将POST请求转换为PUT或DELETE请求 -->
    <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>

3.springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 扫描组件 -->
    <context:component-scan base-package="com.atguigu.controller"></context:component-scan>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

4.rest.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>
    <a href="testREST/1001">测试GET请求</a>
    <br>
    <form action="testREST" method="POST">
        <input type="submit" value="测试POST"/>
    </form>
    <br>
    <form action="testREST" method="POST">
        <input type="hidden" name="_method" value="PUT"/>
        <input type="submit" value="测试PUT"/>
    </form>
    <br>
    <form action="testREST/1001" method="POST">
        <input type="hidden" name="_method" value="DELETE"/>
        <input type="submit" value="测试DELETE"/>
    </form>
</body>
</html>

5.success.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>
    <h1>成功</h1>
</body>
</html>

6.RESTController.java

package com.atguigu.controller;

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;

@Controller
public class RESTController {
    @RequestMapping(value="/testREST/{id}", method=RequestMethod.GET)
    public String getUserById(@PathVariable("id")Integer id){
        System.out.println("GET,id="+id);
        return "success";
    }
    
    @RequestMapping(value="/testREST",method=RequestMethod.POST)
    public String insertUser(){
        System.out.println("POST");
        return "success";
    }
    
    @RequestMapping(value="/testREST",method=RequestMethod.PUT)
    public String updateUser(){
        System.out.println("PUT");
        return "success";
    }
    
    @RequestMapping(value="/testREST/{id}",method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable("id")Integer id){
        System.out.println("DELETE,id="+id);
        return "success";
    }
}