controller与requestmapping
使用@controller定义controllers
Spring mvc将特定url的请求分发到controller类来进行处理
在spring 3.0中,通过@controller标注即可将class定义为一个controller类。为使spring能找到定义为controller的bean,需要在spring-context配置文件中增加如下定义.
<context:component-scan base-package="net.zhepu.web" />
使用@RequestMapping标注来关联url和controllers
通过@RequestMapping标注,可以将特定的url和具体的controller类或controller类中的方法绑定。如
@Controller
@RequestMapping("/helloworld")
public class Helloworld { @RequestMapping(method=RequestMethod.GET)
public ModelAndView hello() {
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
}
}
将/hellowrold和 Hellowrold这个controller类绑定,而在hello()方法前的@RequestMapping(method=RequestMethod.GET)则将hello这个方法和/hellorworld.action的get请求绑定起来。
定义multiaction controllers
使用一个controller来处理多个action,称为multiaction controller。@RequestMapping标注并不要求一定放在class定义之前,而可以直接作为一个method level的标注来使用,当这样使用时,contorller类就变成了一个multiaction controller,例如
@Controller
public class MultiactionHelloworld { @RequestMapping("/hello1")
public ModelAndView hello1(){
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
} @RequestMapping("/hello2")
public ModelAndView hello2(){
ModelAndView mv = new ModelAndView();
mv.setViewName("helloworld");
return mv;
}
这里分别定义了两个方法hello1和hello2,对应的url为/hello1和/hello2
url template
@requestmapping参数中的url,除了常规的url外,也可以使用url template来定义形成类似REST请求的url。
例如
@RequestMapping("/urltemplate/{username}")
public ModelAndView test1(@PathVariable String username){
ModelAndView mv = new ModelAndView();
mv.addObject("userName", username);
System.out.println(username);
mv.setViewName("test002");
return mv;
}
<span style="white-space: normal;">
</span>
以上通过@PathVariable将入参中的username和userid分别与请求url中的{username}和{userid}的值做绑定
@requestmapping的可选参数
value:表示需要匹配的url的格式。
method:表示所需处理请求的http 协议(如get,post,put,delete等),可选值为RequestMethod这个enum的值。
params:格式为”paramname=paramvalue” 或 “paramname!=paramvalue”。不带参数则表示paramvalue可以为任意值。
如params = {"param1=1","param2!=2","param3"},表示对应的url必须包括param1,param2,param3三个参数,其中param1的值必须为1,param2的值不能为2,param3的值可以为任意值。
headers:用来限定对应的reqeust请求的headers中必须包括的内容,例如
headers={"Connection=keep-alive"}, 表示请求头中的connection的值必须为keep-alive。
controller与requestmapping的更多相关文章
- springmvc常用注解之@Controller和@RequestMapping
对于各种注解而言,排第一的当然是“@Controller”,表明某类是一个controller. “@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- SpringBoot-URL路由:@Controller和@RequestMapping
SpringBoot定义URL处理方法:@Controller和@RequestMapping @Controller标注的类表示的是一个处理HTTP请求的控制器(即MVC中的C),该类中所有被@Re ...
- 【#】Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释
Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释 博客分类: spring MVCSpringWebXMLBean 一:配置 ...
- Spring中Controller和RequestMapping的详解
先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...
- Spring MVC基本配置@controller和@RequestMapping解释
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...
- Controller和RequestMapping
一.Controller返回值,String或者ModelAndView 首先看一下spring的配置文件,如下: 第一种,返回类型为String,Controller中的方法如下: 根据 ...
- SpringMVC中的@Controller和@RequestMapping到底什么鬼?
1.1 @Controller是什么 首先看个例子: @Controller @RequestMapping("/blog") public class BlogControlle ...
随机推荐
- Endv 博客简介
Endv 博客简介 此博客收集了 C++.Android.C#.java.php 等优秀的开源项目, 同时也对 Windows.Linux.CentOS 等系列操作系统的开发环境.办公环境的配置写了一 ...
- MFC画图总结-DIB图形绘制
參考文档: http://blog.csdn.net/hnust_xiehonghao/article/details/37652927 http://blog.sina.com.cn/s/blog_ ...
- 缺少dll文件的解决方法
1.什么是dll文件 从专业的角度来说,dll文件,即动态连接库,是一种不可执行的二进制文件,它允许程序共享执行特殊任务所必需的代码和其他资源.打个比方,相当于你去饭店吃饭,只人带上钱或卡就可以了,不 ...
- 安装使用ionic3
1.安装ionic3 $ npm install -g ionic@latest 2.创建ionic3项目 $ ionic start myNewProject blank 3.启动ionic3项目 ...
- cp 命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/10/29/2744185.html cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情 ...
- VMWare虚拟机“锁定文件失败“怎么办?
虚拟机突然蓝屏了,然后就启动不了了,提示"锁定文件失败,打不开磁盘或快照所依赖的磁盘"的解决方法: 如果使用VMWare虚拟机的时候突然系统崩溃蓝屏,有一定几率会导致无法启动,会提 ...
- java面试第七天
反射: 反射:在运行时动态分析或使用一个类进行工作. java.lang.Class类:描述类信息的类. 类对象:描述一个类信息的对象,当虚拟机加载类的时候,就会创建这个类的类对象并加载该对象,Cla ...
- elasticsearch5.4体验
Elasticsearch是基于Lucene分布式.实时查询的搜索分析引擎.我目前应用在日志集中化处理上... 01.下载elasticsearch5.4 https://www.elasti ...
- Centos定时执行python脚本
其实就是linux的定时任务.老记不住参数,这次写下来,省着老百度.本文没有技术含量,请大家不要吐槽. ================================================ ...
- 【TP3.2】跨库操作和跨域操作
一.跨库操作:(同一服务器,不同的数据库) 假设UserModel对应的数据表在数据库user下面,而InfoModel对应的数据表在数据库info下面,那么我们只需要进行下面的设置即可. class ...