Spring框架学习(5.1)SpringMVC常用注解-请求映射方式

前言

记录学习过程,上接基于注解的处理器

SpringMVC可以根据请求方式、Ant风格的URl路径和REST风格的URL路径进行映射

请求映射方式

通过method属性设置请求映射方式
(1)在HelloController方法中加一个POST请求映射

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/springmvc")
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("HelloWorld");
        return "success";
    }
    @RequestMapping(value = "/requestMethod",method = RequestMethod.POST)
    public String requestMethod(){
        return "RqMethodPost";
    }
}

(2)在index.jsp中加一个超链接

 <body>
    <a href="/springmvc/hello">Hello SpringMVC</a>
    <a href="/springmvc/requestMethod">通过Post请求</a>
  </body>

(3)编写一个RqMethodPost.jsp

<body>
    请求方法为:POST
</body>

(3)测试发现失败,因为超链接发出的请求为GET方法,而前面的方法设置为处理Post方法的请求
在这里插入图片描述
GET和POST是什么?HTTP协议中的两种发送请求的方法

对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);
而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。
大佬的通俗易懂的解释
(4)通过表单设置Method

 <body>
    <a href="/springmvc/hello">Hello SpringMVC</a><br>
    <form action="/springmvc/requestMethod" method="post">
      <input type="submit" value="Requset Method: POST">
    </form>

  </body>

测试成功
在这里插入图片描述

Ant风格的URL路径映射

Ant风格的URl支持“?”、“”、“**”三种匹配符
“?”符号匹配文件名中的一个字符:/ab?.jsp可以匹配/abc.jsp、/abd.jsp,但不能匹配/abcd.jsp
“ * ” 符号匹配文件名中的任意字符:/
/ab.jsp可以随意写一个目录名,/a/abc.jsp
“ ** ”符号匹配多层路径:**/ab.jsp可以多个目录,/a/b/ab.jsp,/a/ab.jsp…
例:
(1)在HelloController类中添加方法

  @RequestMapping("/*/pathAnt")
    public String pathAnt(){
        System.out.println("Ant风格的URL路径映射");
        return "success";
    }

“**”符号

 @RequestMapping("**/pathAnt")
    public String pathAnt(){
        System.out.println("Ant风格的URL路径映射");
        return "success";
    }

(2)index.jsp中添加超链接

 <a href="/springmvc/myAnt/pathAnt">Path Ant</a>

用"**"符号

<a href="/springmvc/a/b/c/pathAnt">Path Ant</a>

(3)测试成功
在这里插入图片描述

REST风格的URL路径映射

REST(表现层状态转化)是一种互联网软件架构,采用REST风格可以有效降低开发的复杂性,提高系统的可伸缩性

REST使用HTTP协议连接器来标识对资源的操作(获取/查询,创建、删除、修改),用HTTP Method确定操作方式

  • HTTP GET标识获取和查询资源
  • HTTP POST标识创建资源
  • HTTP PUT标识修改资源
  • HTTP DELETE标识删除资源

“/user/1 HTTP GET”表示获取id为1的user对象

URL加上HTTP Method构成了REST风格数据处理的核心,URL确定操作的对象,HTTP Method确定操作的方式
(form表单只支持get和post,Spring提供了一个过滤器HiddenHttpMethodFilter,可以将DELETE和PUT请求转换为标准的HTTP方式,能将POST请求转为DELETE或PUT请求)

例:
(1)在web.xml中配置过滤器HiddenHttpMethodFilter

 <!--配置HiddenHttpMethodFilter,将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>

(2)处理GET、POST请求
在index.jsp添加超链接

<a href="/springmvc/rest/99">Rest GET</a><br><br>
    <form action="/springmvc/rest" method="post">
      <input type="submit" value="Rest POST">
    </form>

(3)在HelloController类添加restGET、restPOST方法

    @RequestMapping(value = "rest/{id}",method = RequestMethod.GET)
    public String restGET(@PathVariable("id") Integer id){
        System.out.println("Rest GET:"+id);
        return "success";
    }
    @RequestMapping(value = "/rest",method = RequestMethod.POST)
    public String restPOST(){
        System.out.println("Rest POST");
        return "success";
    }

(4)测试运行成功
在这里插入图片描述
(5)处理DELETE、PUT请求
表单使用一个名称为“_method”的隐藏域,并给其赋值为DELETE,过滤器HiddenHttpMethodFilter正是通过“_method”的值将POST请求转为DELETE
PUT同理

form action="/springmvc/rest/1" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit" value="Rest DELETE"> 
  </form>
  <form action="/springmvc/rest/88" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="submit" value="Rest PUT">
  </form>

(6)在HelloController添加方法

 @RequestMapping(value = "/rest/{id}",method = RequestMethod.DELETE)
    public String restDELETE(@PathVariable("id") Integer id){
        System.out.print("Rest DELETE:"+id);
        return "redirect:/springmvc/doTransfer";
    }
 @RequestMapping(value = "/rest/{id}",method = RequestMethod.PUT)
    public String restPUT(@PathVariable("id") Integer id){
        System.out.println("Rest PUT:"+id);
        return "redirect:/springmvc/doTransfer";
    }

restDELETE、restPUT方法返回值使用重定向,将请求重定向到springmvc/doTransfer,该映射处理方法为:

  @RequestMapping("/doTransfer")
    public String doTransfer(){
        return "success";
    }

(7)测试成功
在这里插入图片描述
在这里插入图片描述

发布了49 篇原创文章 · 获赞 0 · 访问量 1262

猜你喜欢

转载自blog.csdn.net/key_768/article/details/103953561
今日推荐