• 【SpringMVC入门系列】篇3:@RequestMapping & @RequestHeader & @CookieValue详解与REST风格请求


    一、@RequestMapping

    1.@RequestMapping

    • value:value属性可以用来设置请求路径,值是一个字符串数组,可以设置成多个路径共同访问一个方法,下面代码就是firstsecond请求共同访问show方法。

      @RequestMapping(value={"/first", "/second"})
      public String show(HttpServletRequest request) {
      
      String id = request.getParameter("id");
      String name = request.getParameter("name");
      System.out.println(name + " " + id);
      return "/first.jsp";
      }
      
    • method:用来设置映射的请求方式,值是RequestMethod类型的数组。如果没有写,则没有限制,post与get都可以请求到对应的方法,如果指定了请求类型,则必须得是相应的请求才能访问到对应的方法。

      下面代码表示该方法只接受get请求。

          @RequestMapping(value={"/first", "/second"}, method = RequestMethod.GET)
          public String show(HttpServletRequest request) {
      
              String id = request.getParameter("id");
              String name = request.getParameter("name");
              System.out.println(name + " " + id);
              return "/first.jsp";
          }
      

      下面方法表示接收get、post请求

          @RequestMapping(value={"/first", "/second"}, method = {RequestMethod.GET,RequestMethod.POST})
          public String show(HttpServletRequest request) {
      
              String id = request.getParameter("id");
              String name = request.getParameter("name");
              System.out.println(name + " " + id);
              return "/first.jsp";
          }
      
    • params:必须设置对应的请求参数和请求值才能访问到对应的内容,下面代码表示请求中必须要有name=thinmoon,而且id不等于1才能访问到方法。

      @RequestMapping(value = "/second", params = {"name=thinmoon", "id!=1"})
      public String show2(@RequestParam(value = "id") Integer idKey , String name) {
      
      System.out.println(name + " " + idKey);
      return "/first.jsp";
      }
      
    • headers:发送的请求头必须要与设置的请求相同时,才能够访问到对应的方法

      下面代码表示只有主机为localhost,并且是http://localhost/页面发来的请求才接收

      @RequestMapping(value = {"/first"} , headers = {"Host=localhost", "Referer=http://localhost/"})
      public String show(HttpServletRequest request) {
      
      String id = request.getParameter("id");
      String name = request.getParameter("name");
      System.out.println(name + " " + id);
      return "/first.jsp";
      }
      
    • Ant风格地址

      我们请求路径可以使用Ant风格的地址,采用通配符的方式进行地址匹配

      ?:一个?匹配一个字符
      *:匹配任意字符
      **:匹配多重路径

    二、REST请求风格

    我们平时使用的请求都是get请求后面加参数的形式,例如:

    <a href="${pageContext.request.contextPath}/first?id=101&name=thinmoon">跳转</a>
    

    REST风格请求

    • REST 即 Representational State Transfer (资源)表现层状态转化
    • 用URL定位资源,用HTTP描述操作
    • 是目前最流行的一种互联网软件架构
    • 它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用
    • 使用POST, DELETE, PUT, GET 分别对应 CRUD
    • Spring3.0 开始支持 REST 风格的请求

    传统的CRUD操作我们一般使用以下形式:

    http://localhost:8080/get.action?id=10  	  查询 get
    http://localhost:8080/add.action              新增 post
    http://localhost:8080/update.action           修改 post
    http://localhost:8080/delete.action?id=10     删除 post
    

    使用rest风格我们可以写成以下形式:

    http://localhost:8080/goods/1		查询GET
    http://localhost:8080/goods			新增POST
    http://localhost:8080/goods			更新PUT
    http://localhost:8080/goods/1		删除DELETE
    

    可以看到,我们通过url直接定位到资源,然后利用请求方式的不同进行CRUD操作。

    rest风格请求使用

    假设我们有get请求如下:

    <a href="${pageContext.request.contextPath}/first/1">跳转first</a>
    

    我们可以在控制器中这样接收:

    @RequestMapping("/first/{id}")
    public String show(@PathVariable Integer id) {
    
        System.out.println(id);
        return "/first.jsp";
    }
    

    form表单put与delete请求

    默认情况下Form表单是不支持PUT请求和DELETE请求的,spring3.0添加了一个过滤器HiddenHttpMethodFilter,可以将post请求转换为PUT或DELETE请求。

    在web.xml中配置如下过滤器:

    <!--配置HiddenHttpMethodFilter 实现rest风格请求-->
    <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>
    

    假如我们有以下form表单请求:注意要使用POST

    <form action="${pageContext.request.contextPath}/rest" method="post">
        <%--定义一个隐藏表单 name值必须为_method value为请求方式--%>
        <input type="hidden" name="_method" value="put">
        <input type="text" name="name" value="Thinmoon">
        <input type="submit" value="提交">
    </form>
    

    我们在表单中定义一个隐藏表单,是我们程序可以使用put请求方式接收。

    @RequestMapping(value = "/rest", method = RequestMethod.PUT)
    public String show5(String name) {
    System.out.println(name);
    return "toAnother";
    }
    
    @RequestMapping("toAnother")
    public String show6() {
    return "redirect:first.jsp";
    }
    

    注入事项:

    上面代码如果直接请求转发到jsp页面会发生405错误 JSPs only permit GET POST or HEAD**

    解决方式:

    • 使用重定向的形式跳转到对应jsp
    • 直接把对应jsp的 isErrorPage="true"

    三、@RequestHeader & @CookieValue

    我们可以通过@RequestHeader和@CookieValue获取请求头与cookie信息

    @RequestMapping("testHeader")
    public String show7(@RequestHeader("Host") String host,
    @RequestHeader("Referer") String referer,
    @RequestHeader("Cookie") String cookie,
    @CookieValue("JSESSIONID") String jsessionid) {
    System.out.println(host);
    System.out.println(referer);
    System.out.println(cookie);
    System.out.println(jsessionid);
    return "first.jsp";
    }
    
  • 相关阅读:
    Ubuntu18.04安装Virtualenv虚拟环境
    SQLite3学习笔记----创建数据库的两种方式
    Git学习笔记-----下载GitHub上某个分支的代码
    Git学习笔记——从一台电脑上传文件到Github上
    plsql配置数据库连接
    Java与各种数据库连接代码
    marquee上下左右循环无缝滚动代码
    仅用aspx文件实现Ajax调用后台cs程序。(实例)
    MVC 5使用TempData(对象)跨视图传递数据
    SQL Server中查询数据库及表的信息语句
  • 原文地址:https://www.cnblogs.com/ThinMoon/p/13246970.html
Copyright © 2020-2023  润新知