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 这样,现在不用这样,这样很方便,今天我们就讲到这里。

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

注解@RequestMapping 的使用的更多相关文章

  1. SpringMVC使用注解@RequestMapping映射请求

    pringMVC通过使用@RequestMapping注解,实现指定控制器可以处理哪些URL请求. 控制器的类定义及方法定义处都可以标注@RequestMapping: 类定义处:提供初步的请求映射信 ...

  2. 注解 @RequestMapping

    通过RequestMapping注解可以定义不同的处理器映射规则. 1.URL路径映射 @RequestMapping(value="/item")或@RequestMapping ...

  3. SpringMVC注解@RequestMapping之produces属性导致的406错误

    废话不多说,各位,直接看图说话,敢吗?这个问题网上解决的办法写的狠是粗糙,甚至说这次我干掉它完全是靠巧合,但是也不否认网上针对406错误给出的解决方式,可能是多种情况下出现的406吧?我这次的流程就是 ...

  4. SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析

    SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...

  5. 注解@RequestMapping value 用法

    本文引自:https://blog.csdn.net/qq_33811662/article/details/80864784 RequestMapping是一个用来处理请求地址映射的注解,可用于类. ...

  6. SpringMVC注解@RequestMapping全面解析---打酱油的日子

    @RequestMapping 可以出现在类级别上,也可以出现在方法上.如果出现在类级别上,那请求的 url 为 类级别上的@RequestMapping + 方法级别上的 @RequestMappi ...

  7. SpringMVC注解@RequestMapping

        /**      * GET 查询      *      * @return 视图路径      */     @RequestMapping(value = {"/index&q ...

  8. Ajax Post提交事例及SpringMVC注解@RequestMapping取不到参数值解决办法

    var xmlHttp; //定义变量,用来创建xmlHttp对象 function ajaxfunction(url,onreadystatechangMethod,param){ // 创建xml ...

  9. springMVC请求注解@RequestMapping各个属性值

    最近遇到了一个采用fastJson传输数据的方式,搞了半天,总是感觉模糊,觉得自己有必要在这里做一个系统的总结,今天先从@RequestMapping的属性开始,采用REST 风格的 URL 请求,R ...

随机推荐

  1. (转)为什么所有浏览器的userAgent都带Mozilla

    转自:http://www.eamonning.com/blog/view/289 以下是全文 最早的时候有一个浏览器叫NCSA Mosaic,把自己标称为NCSA_Mosaic/2.0 (Windo ...

  2. (转)MySQL索引原理及慢查询优化

    转自美团技术博客,原文地址:http://tech.meituan.com/mysql-index.html 建索引的一些原则: 1.最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到 ...

  3. linux系统下修改文件夹目录权限

    linux系统下修改文件夹目录权限 文件夹权限问题 Linux.Fedora.Ubuntu修改文件.文件夹权限的方法差不多.很多人开始接触Linux时都很头痛Linux的文件权限问题.这里告诉大家如何 ...

  4. 多光谱图像数据库, Multispectral images databses

    1. https://scien.stanford.edu/index.php/hyperspectral-image-data/ 2. http://www.cs.columbia.edu/CAVE ...

  5. reset.css css重置公共样式

    @charset "utf-8";/*Css Document*/ /*! * @名称:reset.css * @功能:1.重设浏览器默认样式 * 2.设置通用原子类 *//* 防 ...

  6. Redis总结(四)Redis 的持久化

    前面已经总结了Redis 的安装和使用今天讲下Redis 的持久化. redis跟memcached类似,都是内存数据库,不过redis支持数据持久化,也就是说redis可以将内存中的数据同步到磁盘来 ...

  7. [转] Unity Mathf 数学运算(C#)

    Mathf.Abs 绝对值 计算并返回指定参数 f 绝对值. Mathf.Acos 反余弦 static function Acos (f : float) : float 以弧度为单位计算并返回参数 ...

  8. C:Wordpress自定义文章类型(图视频)

    自定义文章类型,包括: 1:单独的"文章内容模板" 2:单独的"文章列表模板" 3:单独的"控制后台"(文章分类.添加文章) 创建自定义文章 ...

  9. 处理 input 上传图片,浏览器读取图片大小过程中遇到到的坑(兼容IE8\9)

    为了 解决这个坑~ 已经 累傻了.. 周末再 写吧..

  10. Android检测网络是否正常代码!

    在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...