使用@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的更多相关文章

  1. springmvc常用注解之@Controller和@RequestMapping

    对于各种注解而言,排第一的当然是“@Controller”,表明某类是一个controller. “@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访 ...

  2. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  3. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  4. SpringBoot-URL路由:@Controller和@RequestMapping

    SpringBoot定义URL处理方法:@Controller和@RequestMapping @Controller标注的类表示的是一个处理HTTP请求的控制器(即MVC中的C),该类中所有被@Re ...

  5. 【#】Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释

    Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释 博客分类:  spring MVCSpringWebXMLBean  一:配置 ...

  6. Spring中Controller和RequestMapping的详解

    先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...

  7. Spring MVC基本配置@controller和@RequestMapping解释

    一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml   dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...

  8. Controller和RequestMapping

    一.Controller返回值,String或者ModelAndView       首先看一下spring的配置文件,如下: 第一种,返回类型为String,Controller中的方法如下: 根据 ...

  9. SpringMVC中的@Controller和@RequestMapping到底什么鬼?

    1.1 @Controller是什么 首先看个例子: @Controller @RequestMapping("/blog") public class BlogControlle ...

随机推荐

  1. [NPM] Use npx to run commands with different Node.js versions

    We will use npx to run a package using different versions of Node.js. This can become valuable when ...

  2. bash參考手冊之六(Bash特性)

    6 Bash 特性 这部分描写叙述Bash独有的特性. *  调用Bash : Bash能够接受的命令行选项. *  Bash启动文件 : Bash何时及怎样运行脚本. *  交互Shell : 什么 ...

  3. Hibernate常用查询语句

    Hibernate常用查询语句 Hib的检索方式1'导航对象图检索方式.通过已经加载的对象,调用.iterator()方法可以得到order对象如果是首次执行此方法,Hib会从数据库加载关联的orde ...

  4. php之快速入门学习-2

    创建(声明)PHP 变量 PHP 没有声明变量的命令. 变量在您第一次赋值给它的时候被创建: <?php $txt="Hello world!"; $x=5; $y=10.5 ...

  5. Install-DedupCore.component 的内容

    [amd64_microsoft-windows-dedup-common_31bf3856ad364e35_6.3.9600.16384_none_24924b7b049f1064] CF=0000 ...

  6. PHP匿名函数如何理解,什么是匿名函数

    揭秘PHP匿名函数 定义:匿名函数就是没有名字的函数. 有2种形式的匿名函数: 形式1:将一个匿名函数"赋值"给一个变量——此时该变量就代表该匿名函数了! 形式2: 是直接将一个匿 ...

  7. Java读取文件整理

    /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public class ReadFromFile { public static void readFileB ...

  8. c#实现统计代码运行时间

    方法一: //实例化一个计时器 Stopwatch watch = new Stopwatch(); //開始计时 watch.Start(); //此处为要计算的执行代码 for (int i = ...

  9. 数据库选型之亿级数据量并发访问(MySQL集群)

    刘 勇  Email:lyssym@sina.com 简介 针对实际应用中并发访问MySQL的场景,本文采用多线程对MySQL进行并发读取访问,其中以返回用户所需的数据并显示在终端为测试结束节点,即将 ...

  10. 在交叉编译中使用最新版的SS

    因为旧版本的ss-local总是出现 shake hands failed 错误, 打算用最新的版本试试, 所以尝试在编译中使用最新版的shadowsocks. 项目地址 Shadowsocks-li ...