• 注解@RequestMapping 的使用


    1首先@RequestMapping 中的值,我们说请求方法l路径,请求url我们都知道怎么请求了,在第一节helloworld中,

    我们先说我们先建一个类,RequestMappingTest 

    方法如下:

    /**
    * 1. @RequestMapping 除了修饰方法, 还可来修饰类 2. 1). 类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录
    * 2). 方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL
    * 相对于 WEB 应用的根目录
    */

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

    方法名字和@RequestMapping中的值一样,这里面可以有两种方式:

    1当我们在类中RequestMappingTest 上可以加注解也可以不加注解@RequestMapping

    当我们加注解的时候比如 @RequestMapping("/springMvc")

    那么我们超链接的请求方式为   <a href="springMvc/requestMapping">springMvc/requestMapping</a><br/>

    如果不加注解     <a href="requestMapping">requestMapping</a><br/>  

    2.我们可以使用 method 属性来指定请求方式

    @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public String testMethod() {
    System.out.println("testMethod");
    return SUCCESS;
    }

    那么我们超链接的请求方式为

    <!--因为被注解是Post请求,本身请求是get,所以请求不到 -->
    <a href="springMvc/testMethod">springMvc/testMethod</a><br/>

    这样是错误的,正确的方法是要post请求,那么我们可以建一个表单

    <form action="springMvc/testMethod" method="post">
    <input type="submit">

    3.可以使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式

    /**
    *  可以使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式.
    *
    * @return
    */
    @RequestMapping(value = "testParamsAndHeaders", params = { "username",
    "age!=10" }, headers = { "Accept-Language=en-US,zh;q=0.8" })
    public String testParamsAndHeaders() {
    System.out.println("testParamsAndHeaders");
    return SUCCESS;
    }

    那么我们超链接的请求方式为 

    <a href="springmvc/testParamsAndHeaders?username=atguigu&age=10">Test ParamsAndHeaders</a>
    <br><br>  ,headers中的Accept-Language=en-US,zh;q=0.8,我们可以用火狐浏览器按F12可以看到,这个是http协议中

    的请求头中的内容,我们也可以指定其他的,我们看下这个超链接肯定是错误的,因为我们方法中设置的是age!=10,而我们是age=10

    所以,改过了就可以咯

    4@PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.

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

    我们超链接的请求方式为 :

    <a href="springMvc/testPathVariable/101">springMvc/testPathVariable/101</a><br/>

    5@RequestMapping 还 支持 Ant  格 URL 

    @RequestMapping("/testAntPath/*/abc")
    public String testAntPath() {
    System.out.println("testAntPath");
    return SUCCESS;
    }

    我们超链接的请求方式为 :

      <a href="springMvc/testAntPath/ddd/abc">springMvc/testAntPath</a><br/>,ddd的值可以是任意的

     6.我们来说下rest

    我们可以先建一个jsp代码如下:

    <form action="springMvc/testRestPut/1" method="Post">
    <input type="hidden" name="_method" value="PUT">
    <input type="submit" value="testRestPut">

    </form><br>

    <form action="springMvc/testRestDelete/1" method="Post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit" value="testRestDelete">

    </form><br>

    <form action="springMvc/testRestPost/1" method="Post">
    <input type="submit" value="testRestPost">

    </form><br>

    <a href="springMvc/testRestGet/1">testRestGet</a><br/>

    我们用方法来测试一下:

    @RequestMapping(value="/testRestPut/{id}",method=RequestMethod.PUT)
    public String testRestPut(@PathVariable Integer id){
    System.out.println("testRestPut()..."+id);
    return SUCCESS;
    }
    @RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE)
    public String testRestDelete(@PathVariable Integer id){
    System.out.println("testRestDelete()..."+id);
    return SUCCESS;
    }
    @RequestMapping(value="/testRestPost/{id}",method=RequestMethod.POST)
    public String testRestPost(@PathVariable Integer id){
    System.out.println("testRestPost()..."+id);
    return SUCCESS;
    }

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

    一一跟上面对应,我们来说明一下,我们知道本来就只有get,post请求,get,post请求我们就不说了

    如何将post请求转换成DELETE,和PUT呢,我们要建一个表单,这样才能是post请求,

    如果要转换,那么我们要在表单中加入隐藏域,name属性必须为 _method,value的值可以是

    DELETE,PUT,为什么是这样呢,我们要有个关键的细节没做,做了在说;我们要在web.xml中加入

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

    ,我们可以看下HiddenHttpMethodFilter 的源码,Alt +Shirt+T 进入后你们看到有个常量是_method,

    所以隐藏域中的name属性必须是name属性必须为 _method,value是post要转换成的值,源码有兴趣的朋友

    可以看下,value大小写都没关系,源码中会全部转换成大写,

    这样我们用@PathVariable注解就可以传入参数,这样我们就可以进行CRUD了,以前我们学习

    webservlet的时候 一般都是delete?id=1 这样,现在不用这样,这样很方便,今天我们就讲到这里。

    希望看我博客的朋友继续关注哦,我会把所有学习到的知识全部更在博客上。

  • 相关阅读:
    tips for Flask
    REST
    数据结构与算法分析 in C语言
    大学环境对大学生学习心态的影响
    Qpython_文本读写_工作目录
    The Zen of Python
    SQL SERVER数据库中DDL语句
    sql server创建序列sequence
    macbook 安装redis流程及问题总结
    mac系统chrome浏览器快捷键
  • 原文地址:https://www.cnblogs.com/jrts/p/6158950.html
Copyright © 2020-2023  润新知