@RequestMapping映射请求注解
在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求
1.在控制器的类和方法上都可以加此注解
a.加在类上:提供初步的请求映射信息。相对于 WEB 应用的根目录
b.加在方法上:提供进一步的细分映射信息。相对于标记在类上的 URL。
注意:类上未标注 @RequestMapping,则方法处标记的 URL 相对于 WEB 应用的根目录。
作用:DispatcherServlet 截获请求后,就通过控制器上 @RequestMapping 提供的映射信息确定请求所对应的处理方法。
package com.blh.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
/*
@RequestMapping:设置请求映射,把请求和控制层中的方法设置映射关系
当请求路径和RequestMapping的value值一致时,则该注解所标注的方法即为请求处理的方法
RequestMapping:可以加在类上也可以加在方法上
若类和方法上都有,则应该一层一层的访问,先访问类在访问类中的方法
method:用来设置请求方式,当客户端发送的请求方式和method的值一致时,才能处理请求
请求方式:GET查询,POST添加,PUT修改,DELETE删除
params:用来设置客户端传到服务器的数据,支持表达式
headers:用来设置请求头信息,所发送的请求头信息一定要 和headers属性的值设置成一样
*/
@RequestMapping(value="/test",method = RequestMethod.GET,params = {"username","age!=12"})
public String test() {
System.out.println("success:get");
return "success";
}
/*
springMVC支持ant方式的请求路径
在ant中有三种匹配符
*:任意字符
?:任意一个字符
**:任意多层目录
*/
@RequestMapping(value="/*/ant??/**/testAnt")
public String test1() {
System.out.println("success:post");
return "success";
}
@RequestMapping("testREST/{id}/{username}")
public String testREST(@PathVariable("id")Integer id,@PathVariable("username")String username) {
System.out.println("id"+id+"username:"+username);
return "success";
}
}