SpringMVC学习 -- 使用 @RequestMapping 映射请求
在控制器的类定义及方法出定义出都可以标注 @RequestMapping:
- 类定义处:提供初步的请求映射信息。相对于 Web 应用的根目录。
- 方法定义出:提供进一步的细分映射信息。相对于类定义处的 URL。若类定义处未标注 @RequestMapping , 则方法定义处标记的 URL 相对于 Web 应用的根目录。
DispatcherServlet 截获请求后 , 就通过控制器上 @RequestMapping 提供的映射信息确定请求所对应的处理方法。
web.xml
<?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"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描自定义包 -->
<context:component-scan base-package="com.itdoc.springmvc"/> <!-- 配置试图解析器: 把 Controller 返回值解析成实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
TestRequestMapping.java
package com.itdoc.springmvc; 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; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE RequestMapping 测试
* @AUTHOR WángChéngDá
* @DATE 2017-03-08 14:30
*/
@Controller
@RequestMapping("/springmvc")
public class TestRequestMapping { private final static String SUCCESS = "success"; /**
* 1.@RequestMapping 除了修饰方法还可以修饰类。
* 2.修饰类和修饰方法
* 1) 修饰类: 提供初步的请求映射信息, 相对于 WEB 应用的根目录。
* 2) 修饰方法: 提供进一步细化的请求映射信息, 相对于修饰类处的 URL。
* 若修饰类处未标注 @RequestMapping, 则修饰方法处的 URL 相对于 WEB 应用的根目录。
*/
@RequestMapping("/testreqmap")
public String testReqMap() {
System.out.println("I am TestRequestMapping's testReqMap method...");
return SUCCESS;
}
}
映射请求参数、请求方式或请求头

@RequestMapping 的 value , method , params 及 headers 分别表示请求 URL , 请求方式 , 请求参数及请求头的映射条件 , 联合使用多个条件可让请求映射更加精确化。
method 请求方式常用有四种:
- method = RequestMethod.POST
- method = RequestMethod.GET
- method = RequestMethod.PUT
- method = RequestMethod.DELETE
package com.itdoc.springmvc; 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; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE RequestMapping 测试
* @AUTHOR WángChéngDá
* @DATE 2017-03-08 14:30
*/
@Controller
@RequestMapping("/springmvc")
public class TestRequestMapping { private final static String SUCCESS = "success"; /**
* 使用 method 属性来制定请求方式。
*
* @return
*/
@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("I am TestRequestMapping's testMethod method...");
return SUCCESS;
}
}
params 和 headers 支持简单的表达式:
- param:表示请求必须包含名为 param 的请求参数。
- !param:表示请求不能包含名为 param 的请求参数。
- param != value: 表示请求包含名为 param 的请求参数 , 但其值不能为 value。
- params 可以有多个参数 , 用逗号隔开。示例:params = {"username", "age!=20"}
package com.itdoc.springmvc; 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; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE RequestMapping 测试
* @AUTHOR WángChéngDá
* @DATE 2017-03-08 14:30
*/
@Controller
@RequestMapping("/springmvc")
public class TestRequestMapping { private final static String SUCCESS = "success"; /**
* 可以使用 params 和 headers 来更加精确的映射请求, params 和 headers 支持简单的表达式。
* @return
*/
@RequestMapping(value = "testParamsAndHeaders", params = {"username", "age!=20"},
headers = {"Accept-Language=zh-CN,zh;q=0.8"})
public String testParamsAndHeaders() {
System.out.println("I am TestRequestMapping's testParamsAndHeaders method...");
return SUCCESS;
}
}
Ant 风格资源地址支持3种匹配符:
- ?:匹配文件名中的一个字符。
- *:匹配文件名中的任意字符。
- **:匹配多层路径。
package com.itdoc.springmvc; 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; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE RequestMapping 测试
* @AUTHOR WángChéngDá
* @DATE 2017-03-08 14:30
*/
@Controller
@RequestMapping("/springmvc")
public class TestRequestMapping { private final static String SUCCESS = "success"; @RequestMapping("/testAndPath/*/abc")
public String testAndPath() {
System.out.println("I am TestRequestMapping's testAntPath method...");
return SUCCESS;
}
}
@PathVariable 映射 URL 绑定占位符:
- 带占位符的 URL 是 Spring3.0 新增的功能 , 该功能在 SpringMVC 的 REST 目标挺进发展过程中具有里程碑的意义。
- 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的{xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
- 注意:@PathVariable("xxx") 中的 xxx 必须与占位符 {xxx} 中的 xxx 相同。
package com.itdoc.springmvc; 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; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE RequestMapping 测试
* @AUTHOR WángChéngDá
* @DATE 2017-03-08 14:30
*/
@Controller
@RequestMapping("/springmvc")
public class TestRequestMapping { private final static String SUCCESS = "success"; /**
* @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中。
* @param id
* @return
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("I am TestRequestMapping's testPathVariable method...\t id=" + id);
return SUCCESS;
}
}
SpringMVC学习 -- 使用 @RequestMapping 映射请求的更多相关文章
- SpringMVC之使用 @RequestMapping 映射请求
@RequestMapping注解 SpringMVC使用该注解让控制器知道可以处理哪些请求路径的,除了可以修饰方法,还可以修饰在类上. – 类定义处:提供初步的请求映射信息.相对于 WEB 应用的根 ...
- SpringMVC之使用requestMapping映射请求、映射参数、映射头
1. 映射请求 作用:使用requestMapping可以指定处理器可以处理那些请求 地方:类和方法前面都可以 @requestMapping 类定义处: 提供初步的请求映射信息,相对于web应用的根 ...
- SpringMVC使用注解@RequestMapping映射请求
pringMVC通过使用@RequestMapping注解,实现指定控制器可以处理哪些URL请求. 控制器的类定义及方法定义处都可以标注@RequestMapping: 类定义处:提供初步的请求映射信 ...
- SpringMVC听课笔记(三:使用@RequestMapping映射请求)
1. Spring MVC使用 @RequestMapping 注解为控制器指定可以处理哪些URL请求 2. 标注点: --类定义处:提供初步的请求映射信息.相对于WEB应用的根目录 --方法处:提供 ...
- @RequestMapping映射请求,@PathVariable,@RequestParam,@RequestHeader的使用
1.@RequestMapping Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注. @RequestMa ...
- 用@RequestMapping映射请求
DispatcherServlet接受一个web请求之后,将请求发送给@Controller注解声明的不同控制器类. 这个调度过程依赖控制器类及其处理程序方法中声明的各种@RequestMapping ...
- @RequestMapping映射请求
1.SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求. 2.在控制器的类定义和方法定义处都可标注@RequestMapping 2.1 类定义处:提 ...
- SpringMVC 学习笔记(处理器映射器的配置)
前端控制器(dispatchServlet) 在web.xml中配置前端控制器,在服务器启动时就被创建,用来对请求和响应进行接收 和 分发处理,其在配置时可以设置一个初始化参数,用来定位SpringM ...
- SpringMVC学习笔记一(请求流程和配置,启动项目)
springmvc请求流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映 ...
随机推荐
- urllib,url中链接包含汉字怎么用百分号(%)加密处理
使用urllib中的quote,和unquote方法将汉字编码成gbk(2个百分号对应一个汉字)或者utf8(3个百分号对应一个汉字) 注意用%加密汉字时,汉字不能是Unicode编码格式,否则会报错 ...
- 初步学习pg_control文件之三
接前文,初步学习pg_control文件之二 继续学习: 研究 DBState,先研究 DB_IN_PRODUCTION ,看它如何出现: 它出现在启动Postmaster时运行的函数处: /* * ...
- php杂记——1(基础知识与文件读写)
1.变量前面需要加美元符号"$",常量则不需要: define('PRICE',100); echo PRICE; 2.用一个变量的值作为另一个变量的名称可以得到类似C中的指针变量 ...
- PHP 头像上传
嘻嘻,自从圣诞节过后,就一直懒散,这几天也因为是太过于繁忙的原因,感觉好久都没有出来冒冒泡,诶... 为了生活一直在奋斗,作为一名前端开发工程师,我现在越来越迷茫了,都不知道现在自己到底算什么了? 会 ...
- 使用fiddler对手机上的APP进行抓包
前提: 1.必须确保安装fiddler的电脑和手机在同一个wifi环境下 备注:如果电脑用的是台式机,可以安装一个随身wifi,来确保台式机和手机在同一wifi环境下 安装配置步骤: 1.下载一个fi ...
- kaldi GMM模型解码指令 gmm-latgen-faster详解
目录 - 作用: - 用法: - 可选项及含义: - 使用实例: - 作用: Generate lattices using GMM-based model. 生成基于GMM模型的lattice词格) ...
- JavaScript 面向对象 原型(prototype) 继承
1.对象的概念:无需属性的集合,属性可以为数值,对象或函数,ECMAscript中没有类的概念,这点是javascript与其他面向对象(OO)语言不同的地方. //创建一个自定义对象 var per ...
- input设置为readonly后js设置intput的值后台仍然可以接收到
今天发现一个奇怪现象,一个input属性readonly的值被设置为readonly,然后有前台js给input设置了新值. 虽然前台看不到效果,但是提交到后台后,仍然可以接收到新值,感觉很奇怪. 我 ...
- Android Studio的初体验
在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...
- PAT 1086 就不告诉你
https://pintia.cn/problem-sets/994805260223102976/problems/1038429065476579328 做作业的时候,邻座的小盆友问你:“五乘以七 ...