修饰类:
@RequestMapping("/home1")// 修饰类
@Controller
public class Home {
/**
* 路径 : home1/show1
*/
@RequestMapping("/show1")
public String showPage(HttpServletRequest request){
System.out.println("接收到页面请求");
return "home_index";
}
}指定接收的请求方式,如get、post:
/**
* 指定接收的请求方式,如get、post
* @return
*/
@RequestMapping(value="/testMethod",method=RequestMethod.POST)
public String testMethod(){
return "home_index";
}限制请求参数,请求头:
/**
* 限制请求参数,请求头
* url : home1/testParamsAndHeaders?id=123&age=26
* @return
*/
@RequestMapping(
value="/testParamsAndHeaders",
params={"id","age!=25"},// 有id参数,并且age参数不等于25
headers={"Accept-Language=zh-CN,zh;q=0.8,en;q=0.6,fr;q=0.4"}// 接收含有请求头的请求
)
public String testParamsAndHeaders(){
return "home_index";
}使用通配符:
/*
使用通配符
?:匹配文件名中的一个字符。
*:匹配文件名中任意字符。
**:匹配多层路径
*/
@RequestMapping("/home2/*/1527") // home2/aaa/1527/show2
//@RequestMapping("/home2/a?/1527") // /home2/ab/1527/show2
//@RequestMapping("/home2/**/1527") // home2/a/b/c/1527/show2 home2/a/c/1527/show2
@Controller
public class Home2 {
@RequestMapping("/show2")
public String showPage(HttpServletRequest request){
System.out.println("接收到页面请求");
return "home_index";
}
}映射url占位符到目标方法中的参数:
@RequestMapping("/home3/")
@Controller
public class Home3 {
/**
* 映射url占位符到目标方法中的参数
* 路径 home3/show3/123
* 其中@RequestMapping("/show3/{id}")的"id"要与@PathVariable("id")的"id"名字一致
*/
@RequestMapping("/show3/{id}")
public String showPage(@PathVariable("id") int id){
System.out.println("接收到页面请求,id=" + id);
return "home_index";
}
}