Spring MVC中常用注解之RequestMapping

RequestMapping中有六个属性分别是:

value:     指定请求的实际地址,指定的地址可以是URI Template 模式;
method:  指定请求的method类型, GET、POST、PUT、DELETE等;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

其中比较常用的是value,method,params;


1.RequestMapping最基础的用法就是实现类或者方法的访问控制:例如下面案例就是方法上面的控制:

[html] view plain copy
  1. @RequestMapping(value = "testlogin.do",method = RequestMethod.POST)  
  2. public String testLogin(){  
  3.     return "redirect:login";  
  4. }  
[html] view plain copy
  1. <form id="loginTestForm" action="testlogin.do" method="post">  
  2.     <input type="submit" value="submit">  
  3. </form>  
从代码上面就可以很明显的看出来,在表单中提交action地址,通过SpringMVC框架会自动找到value值等于action地址的方法然后就会执行这个方法。

这里需要注意的是form表单中提交的方式要和方法里面的method一直,否则就会报错。上面的例子中两个都是post方法提交的。

2.RequestMapping带参数的情况:

[html] view plain copy
  1. @RequestMapping(value = "testlogin.do",method = RequestMethod.POST)  
  2.  public String testLogin(@RequestParam("departmentId") String departmentId){  
  3.      System.out.println("输入的参数为:"+departmentId);  
  4.      return "redirect:login";  
  5.  }  
第一种表单形式:
[html] view plain copy
  1. <form id="loginTestForm" action="testlogin.do" method="post">  
  2.     <input type="text" name="departmentId" id="departmentId"></br>  
  3.     <input type="submit" value="submit">  
  4. </form>  
第二中表单形式:
[html] view plain copy
  1. <form id="loginTestForm" action="testlogin.do?departmentId=123" method="post">  
  2.     <input type="submit" value="submit">  
  3. </form>  
以上两种的结果是一样的,都可以执行方法中内容。其中@RequestParam还有其他的写法,我这里就不多介绍了,有兴趣的可以自己去了解一下。

3.RequestMapping中获取URL中的参数信息:

[html] view plain copy
  1. <form id="loginTestForm" action="testlogin.do/adc" method="post">  
  2.     <input type="submit" value="submit">  
  3. </form>  
[html] view plain copy
  1. @RequestMapping(value = "testlogin.do/{departmentId}",method = RequestMethod.POST)  
  2. public String testLogin(@PathVariable("departmentId") String departmentId){  
  3.     System.out.println("输入的URL参数为:"+departmentId);  
  4.     return "redirect:login";  
  5. }  
通过{变量名称}这样的写法,就可以在@PathVariable("变量名")  实现参数的调用了。

当然也可以绑定多个参数,例如:

[html] view plain copy
  1. @RequestMapping(value = "testlogin.do/departmentId/{departmentId}/departmentName/{departmentName}",method = RequestMethod.POST)  
  2. public String testLogin(@PathVariable("departmentId") String departmentId,@PathVariable("departmentName") String departmentName){  
  3.     System.out.println("输入的URL参数为1:"+departmentId);  
  4. tem.out.println("输入的URL参数为2:"+departmentName);  
  5.     return "redirect:login";  
  6. }  
还有就是可以实现正则表达式,我这里就不多介绍了,有兴趣的可以看一下这篇博客:点击打开链接
到这里RequestMapping一些常用的方法就介绍完了,当然还有很多其他的用法。例如:在类中中实现action控制,方法之间相互访问跳转之类的,不过这些方法的实现基本都是基于RequestMapping这些常用方法实现的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值