使用@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。

最新文章

  1. oracle 学习笔记(三)
  2. js遍历json
  3. Tomcat部署web应用程序
  4. JSON 字符串 与 java 对象的转换
  5. Python学习 之 函数
  6. 批量设置AssetBundleName
  7. linux下编译安装nginx
  8. Java IO流之对象流
  9. [国嵌攻略][100][嵌入式Linux内核制作]
  10. Dynamics 365 CE中AsyncOperationBase表记录太多,影响系统性能怎么办?
  11. 谈谈B-树和B+树及其应用
  12. Java代码混淆工具ProGuard
  13. sony Z5P 刷rec、root的方法
  14. winform rar压缩包解压缩
  15. Neural Networks and Deep Learning(week2)Logistic Regression with a Neural Network mindset(实现一个图像识别算法)
  16. poj 3009 冰球 【DFS】求最小步数
  17. 【原创】ABP源码分析
  18. Spring配置文件XML详解
  19. 20135239 Linux内核分析 期中总结
  20. AVAudioFoundation(6):时间和媒体表示

热门文章

  1. Endv 博客简介
  2. 4. Add override methods to class
  3. Python中的乱码
  4. Step by Step iOS Project In Action - 视图控制器
  5. 栈的实现实例(C语言)
  6. js 垃圾回收机制与内存管理
  7. tarfile模块可以方操作tar归档文件
  8. 关于 C# HttpClient的 请求
  9. 域名无法解析 Linux临时或永久修改DNS
  10. Linux下动态共享库加载时的搜索路径详解