SpringMVC -- @PathVariable/Rest风格的URL HiddenHttpMethodFilter (2)

SpringMVC @PathVriable映射URL绑定占位符

带占位符的URL是Spring3.0新增的功能.通过@PathVariable可以讲URL中占位符参数绑定到控制器处理方法的入参中..

还是沿用之前的那个例子的Demo

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: cyx
  Date: 2016/3/30
  Time: 20:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>

      <!-- @PathVariable 映射URL绑定的占位符-->
      <!--
        带占位符的URL是Spring3.0新增的功能.
        通过@PathVariable可以讲URL中占位符参数绑定到控制器处理方法的入参中..
      -->
      <a href="/springmvc/testPathVariable/1">testPathVariable</a>
      <br><br>

      <!-- 创建form表单,模拟POST请求 -->
      <form action="/springmvc/testMethod" method="post">
        <input type="submit" value="submit">
      </form>

      <!--这里先模拟一个get请求,因为RequestMapping设置为只接受POST请求,所以不会应答-->
      <a href="/springmvc/testMethod">testMethod</a>
    <br><br>
      <a href="/springmvc/testRequestMapping">@RequestMapping</a>
    <br><br>
      <a href="helloworld">HelloWorld</a>
  </body>
</html>

然后是方法

package com.springmvc.test;

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;

/**
 * Created by cyx on 2016/3/30.
 */

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {

    private static final String SUCCESS = "success";

    /**
     * 1.@RequestMapping 除了修饰方法,还可以来修饰类 (类上面的就相当于WEB应用中的路径)
     *      http://localhost:8088/springmvc/testRequestMapping
     *
     * 2.类定义处: 提供初步的请求映射信息,相对于WEB目录下的根目录.
     *   方法处:   提供进一步的细分映射信息,相对于类定义处的URL,若类定义处未标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录
     *
     *
     */
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping() {
        System.out.println("testRequestMapping");
        return SUCCESS;
    }



    /**
     *使用method属性来指定请求方式
     */
    @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public String testMethod() {
        System.out.println("testMethod");
        return SUCCESS;
    }


    /**
     * @PathVariable 可以来映射 URL 中的占位符到 目标方法的参数中.
     */
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVaiable(@PathVariable("id") Integer id) {
        System.out.println("testPathVariable :"+id);
        return SUCCESS;
    }

}


结果图

注意 控制台打印信息





//-----------------------------------------------------------------------------------------------------



转换post请求为delete,post请求
1.web.xml文件中配置 org.springframework.web.filter.HiddenHttpMethodFilter
2.index.jsp 写四个请求(post,get,delete,put)
3.控制器中,写四个对应的方法

浏览器form表单只支持GET和POST请求,而DELETE 和 PUT等method 并不支持,spring3.0之后,spring添加了一个过滤器,可以讲这些请求转换为标准的HTTP方法,使得支持GET,POST,PUT,DELETE请求,该过滤器为 HiddenHttpMethodFilter

Rest风格的URL
以CRUD为例:
    新增 :/order POST
    修改 :/order/1 PUT
    获取 :/order1 GET
    删除 :/order/1 DELETE

以前的写法:
    删除: delete?id=1
    获取: get?id=1


还是沿用之前的那个例子......

首先在web.xml配置文件中配置org.spring.framework.web.filter.HiddenHttpMethodFilter

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <!--
        配置org.springframework.web.filter.HiddenHttpMethodFilter
        可以把POST请求转换为 DELETE 或 POST 请求
    -->
    <filter>
        <filter-name>HiddentHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddentHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern><!--过滤所有请求-->
    </filter-mapping>



    <!-- 配置DispathcerServlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置DispatcherServlet 的一个初始化参数: 配置SpringMVC配置文件的位置和名称-->
        <!--
            实际上也可以不通过contextConfigLocation 来配置SpringMVC的配置文件,而使用默认的,
            默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
         -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <!-- 第一次加载的时候就创建,而不是等发送请求的时候才创建 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


然后在index.jsp文件中,写四个模拟请求....

<%--
  Created by IntelliJ IDEA.
  User: cyx
  Date: 2016/3/30
  Time: 20:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>

  <!-- 模拟Rest风格URL -->
    <form action="springmvc/testRest4/1" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="TestRestPUT">
    </form>
  <br><br>
    <form action="springmvc/testRest3/1" method="post">
        <input type="hidden" name="_method" value="DELETE"/>
        <input type="submit" value="TestRestDELETE">
    </form>
  <br><br>
    <form action="springmvc/testRest2" method="post">
        <input type="submit" value="TestRestPost">
    </form>
  <br><br>
    <a href="springmvc/testRest1/1">TestRestGet</a>
  <br><br>




      <!-- @PathVariable 映射URL绑定的占位符-->
      <!--
        带占位符的URL是Spring3.0新增的功能.
        通过@PathVariable可以讲URL中占位符参数绑定到控制器处理方法的入参中..
      -->
      <a href="/springmvc/testPathVariable/1">testPathVariable</a>
      <br><br>

      <!-- 创建form表单,模拟POST请求 -->
      <form action="/springmvc/testMethod" method="post">
        <input type="submit" value="submit">
      </form>

      <!--这里先模拟一个get请求,因为RequestMapping设置为只接受POST请求,所以不会应答-->
      <a href="/springmvc/testMethod">testMethod</a>
    <br><br>
      <a href="/springmvc/testRequestMapping">@RequestMapping</a>
    <br><br>
      <a href="helloworld">HelloWorld</a>
  </body>
</html>

然后在控制器中写相应的调用代码....
package com.springmvc.test;

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;

/**
 * Created by cyx on 2016/3/31.
 */

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTestCode {

    private static final String SUCCESS = "success";

    @RequestMapping("/testRest4/{id}")
    public String TestRestPUT(@PathVariable Integer id) {
        System.out.println("TestRestPUT : "+id);
        return SUCCESS;
    }


    @RequestMapping("/testRest3/{id}")
    public String TestRestDELETE(@PathVariable Integer id) {
        System.out.println("TestRestDELETE : "+id);
        return SUCCESS;
    }


    @RequestMapping("/testRest2")
    public String TestRestPost() {
        System.out.println("TestRestPost");
        return SUCCESS;
    }


    @RequestMapping(value="/testRest1/{id}",method=RequestMethod.GET)
    public String TestRestGet(@PathVariable Integer id) {
        System.out.println("TestRestGet : "+id);
        return SUCCESS;
    }

}

然后就可以欢乐的部署和运行了...


如何发送PUT请求和delete请求?
1.需要配置HiddenHttpMethodFilter
2.需要发送post请求.
3.需要在发送post请求时,携带一个隐藏域(name="_method" value="DELETE")

在SpringMVC目标方法中,如何得到id?
使用@PathVariable注解



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值