Spring MVC中注解的简介
参考网址: https://blog.csdn.net/a67474506/article/details/46361195
@RequestMapping映射请求
SpringMVC 使用 @RequestMapping 注解为控制器指定可以处理那些URL 请求
@RequestMapping
– 类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录.
– 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于WEB 应用的根目录.
DispatcherServlet 截获请求后,就通过控制器上@RequestMapping 提供的映射信息确定请求所对应的处理方法.
@RequestMapping 除了可以使用请求 URL 映射请求外,还可以使用请求方法、请求参数及请求头映射请求.
@RequestMapping限定请求方法、请求参数、请求头
/**
* 接收GET请求
* @return
*/
@RequestMapping(value="/get",method = RequestMethod.GET)
public String get(){
System.out.println("get");
return "get";
} /**
* 接收POST请求
* @return
*/
@RequestMapping(value="/post",method = RequestMethod.POST)
public String post(){
System.out.println("post");
return "post";
} /**
* 只接收 name 参数
* @return
*/
@RequestMapping(value="/params",params="name")
public String params(String name){
System.out.println("hello "+name);
return "helloworld";
} /**
* 只接收请求头中 Content-Type 为 text/html;charset=UTF-8的请求
* @return
*/
@RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")
public String headers(){
System.out.println("headers");
return "helloworld";
}
@RequestMapping匹配符
– ?:匹配文件名中的一个字符
– *:匹配文件名中的任意字符
– **:** 匹配多层路径
实例:
URL : /user/*/create
-- /user/bigsea/create 、 /user/sea/create 等URL
URL : /user/**/create
-- /user/big/sea/create 、 /user/sea/big/create 等URL
URL : /user/create??
-- /user/createaa 、/user/createbb
@PathVariable 注解
带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中
/**
* http://localhost:8080/springmvc/hello/pathVariable/bigsea
* http://localhost:8080/springmvc/hello/pathVariable/sea
* 这些URL都会执行此方法并且将<b>bigsea</b>,<b>sea</b>作为参数,传递到name字段
* @param name
* @return
*/
@RequestMapping("/pathVariable/{name}")
public String pathVariable(@PathVariable("name")String name){
System.out.println("hello "+name);
return "helloworld";
}
@RequestParam 绑定请求参数
在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法
– value:参数名
– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
/**
* 如果 required = true 则表示请求参数对应的 字段 必须存在.如果不存在则会抛出异常<br/>
* @param firstName 可以为null
* @param lastName 不能为null .为null报异常
* @param age age字段表示如果没有 age 参数 则默认值为 0
* @return
*/
@RequestMapping("/requestParam")
public String requestParam(@RequestParam(value="firstName",required=false)String firstName,
@RequestParam( value="lastName" ,required = true) String lastName,
@RequestParam(value="age",required = false ,defaultValue="0")int age) {
System.out.println("hello my name is " + (firstName == null ? "" : firstName)
+ lastName + "," + age +" years old this year");
return "helloworld";
}
@RequestHeader 获取请求头
请求头包含了若干个属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将求头中的属性值绑定到处理方法的入参中
/**
* 获取请求头中的信息
* @RequestHeader 也有value, required, defaultValue三个参数
* @param userAgent
* @param cookie
* @return
*/
@RequestMapping("/requestHeader")
public String requestHeader(@RequestHeader("User-Agent")String userAgent,@RequestHeader("Cookie")String cookie){
System.out.println("userAgent:["+userAgent+"]");
System.out.println("cookie:["+cookie+"]");
return "helloworld";
}
@CookieValue 获取 cookie值
/**
* 使用@CookieValue 绑定cookie值<br/>
* 注解@CookieValue 也有value, required, defaultValue三个参数
* @param session
* @return
*/
public String cookieValue(@CookieValue(value = "JSESSIONID", required= false)String session){
System.out.println("JESSIONID:["+session+"]");
return "helloworld";
}
spring mvc中还有很多的注解, 后面会接着写,
Spring MVC中注解的简介的更多相关文章
- Spring MVC中注解 @ModelAttribute
1.@ModelAttribute放在方法之上,在当前Control内的所有方法映射多个URL的请求,都会执行该方法 @ModelAttribute public void itemsCommon(H ...
- Spring MVC中注解: @ModelAttribute 与@RequestParam区别
相关链接 : https://blog.csdn.net/huang343/article/details/77491096
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- Spring MVC中@RequestMapping注解使用技巧(转)
@RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...
随机推荐
- 通过HBase Shell与HBase交互
出处:http://www.taobaotest.com/blogs/1604 业务开发测试HBase之旅二:通过HBase Shell与HBase交互 yedu 发表于:2011-10-11 浏览: ...
- Spring Boot 高效数据聚合之道
项目地址和示例代码: https://github.com/lvyahui8/spring-boot-data-aggregator 背景 接口开发是后端开发中最常见的场景, 可能是RESTFul接口 ...
- bootstrap常用部件下载
http://shapebootstrap.net/item/1524915-adminlte-dashboard-and-control-panel/live-demo
- Python:列表也能拆包?
前几天,微信学习群里有个小伙伴在看书时遇到了这样一个问题,在群里提问,看下图: 这是常用的 matplotlib 库,只是一般我们调用 plot 方法绘图时,不会去关心它的返回值.然而 plt1, = ...
- 微信小程序小结(3) -- 使用wxParse解析html及多数据循环
wxParse-微信小程序富文本解析组件:https://github.com/icindy/wxParse 支持Html及markdown转wxml可视化 使用 1.copy下载好的文件夹wxPar ...
- 洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle
P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...
- Node.js 内置模块crypto加密模块(4) Diffie Hellman
Diffie-Hellman( DH ):密钥交换协议/算法 ( Diffie-Hellman Key Exchange/Agreement Algorithm ) 百科摘录: Diffie-Hell ...
- Click: 命令行工具神器
Click是一个Python用来快速实现命令行应用程序的包,主要优势表现在以下三点: 任意嵌套命令 自动生成帮助页 自动运行时lazy加载子命令 示例程序: import click @click.c ...
- thinkphp5.1页面页面模板及参数配置
success和error跳转的模板在thinkphp/tpl/dispatch_jump.tpl 配置参数在thinkphp\library\traits\controller\jump.php文件 ...
- layui之layer打开table后分页无效的解决方法
1.原代码: <body> <div id="showalladdableavms" style="display: none;width:100%&q ...