springmvc · 2019-09-05 0

SpringMVC REST风格的请求

REST风格的请求

GET用于获取资源;POST用于增加资源;PUT用于更新资源;DELETE用于删除资源

一、环境搭建

1、添加maven依赖

SpringMVC环境,需要spring依赖的spring-core、spring-beans、spring-context、spring-expression的jar包,spring web依赖的spring-web、spring-webmvc的jar包

spring-webmvc的jarf包依赖以上的包,所以只需在maven添加spring-webmvc依赖,maven也会导入以上的jar包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>

2、配置前端控制器

在web.xml文件中配置DispatcherServlet

<!--
  SpringMVC思想是有一个前端控制器拦截所有请求,并智能派发
  这个前端控制器是一个Servlet,应该在web.xml中配置这个Servlet来拦截所有请求
-->
<servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--指定配置文件位置-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-conf.xml</param-value>
        </init-param>
<!--servlet启动加载,servlet原本是第一次访问创建对象;load-on-startup:服务器启动的时候创建对象;值越小优先级越高,越先创建对象-->
        <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3.配置支持REST风格的filter

在web.xml中配置filter,form表单只能发送get、post请求,需要使用SpringMVC中的HiddenHttpMethodFilter,它可以把普通的请求转换为规定形式的请求,form表单发送的请求先到filter,由filter改变请求方式,实现REST风格的get、post、put、delete请求

form提交参数_method的值为post、put、delete,然后filter就可以识别

<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>

4.spring配置文件

<?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.xsd">

    <!--扫描所有组件-->
    <context:component-scan base-package="com.example"/>

</beans>

二、前端代码

   <a href="/hello/1">查询</a><br/>

   <form action="/hello" method="post">
       <input name="_method" value="post"/>
       <input type="submit" value="增加"/>
   </form><br/>

   <form action="/hello/1" method="post">
       <input name="_method" value="put"/>
       <input type="submit" value="修改"/>
   </form><br/>

    <form action="/hello/1" method="post">
        <input name="_method" value="delete"/>
        <input type="submit" value="删除"/>
    </form><br/>

三、Controller代码

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping(value = "/hello/{id}", method = RequestMethod.GET)
    public String getHello(@PathVariable("id") int id){
        return "this is get";
    }

    @ResponseBody
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String postHello(){
        return "this is post";
    }

    @ResponseBody
    @RequestMapping(value = "/hello/{id}", method = RequestMethod.PUT)
    public String putHello(@PathVariable("id") int id){
        return "this is put";
    }

    @ResponseBody
    @RequestMapping(value = "/hello/{id}", method = RequestMethod.DELETE)
    public String delHello(@PathVariable("id") int id){
        return "this is del";
    }

}