转自:http://www.cnblogs.com/FFFFF/p/4624140.html
使用@PathVariable可以快速的访问,URL中的部分内容。
①. 在@RequestMapping的value中使用URI template({变量名}),然后在@RequestMapping注解方法的需要绑定的参数前,使用@PathVariable指定变量名(如果变量名和参数名一致也可以不指定),从而将URL中的值绑定到参数上。
代码:
1: @RequestMapping("/testPathVariable")
2: @Controller
3: public class TestPathVariable {
4:
5: /*
6: * URI模板指定了一个变量名为id的变量,当控制器处理请求时会将 id 替换为正确的值
7: *
8: * 若请求为 testPathVariable/user/29,则uid=29,输出29
9: *
10: * */
11: @RequestMapping("/user/{id}")
12: public String testPathVariable(@PathVariable("id") Integer uid) {
13: System.out.println(uid);
14: return "success";
15: }
16:
17: }
URL:
1: testPathVariable user 29
②. 一个方法可以有多个@PathVriable注解
URI template 可以这样,全部在方法上
代码:
1: @RequestMapping("/testPathVariable")
2: @Controller
3: public class TestPathVariable {
4:
5: @RequestMapping("/user/{uid}/book/{bid}")
6: public String testMultiplePathVariable(@PathVariable("uid") Integer uid,@PathVariable("bid") Integer bid) {
7: System.out.println(uid);
8: System.out.println(bid);
9: return "success";
10: }
11:
12: }
URL:
1: testPathVariable user 29 book 101
URI template还可以这样, 加在类和方法上
代码:
1: @RequestMapping("/testPathVariable/user/{uid}")
2: @Controller
3: public class TestPathVariable {
4:
5: @RequestMapping("/book/{bid}")
6: public String testMultiplePathVariable(@PathVariable("uid") Integer uid, @PathVariable("bid") Integer bid) {
7:
8: System.out.println(uid);
9: System.out.println(bid);
10: return "success";
11: }
12:
13: }
URL:
1: testPathVariable user 29 book 101
③. @PathVariable 还可以使用在 map 参数上,但是必须配置
代码:
1: @RequestMapping("/testPathVariable/user/{uid}")
2: @Controller
3: public class TestPathVariable {
4:
5: @RequestMapping("/book/{bid}")
6: public String testMultiplePathVariable_Map(@PathVariable Map map) {
7: System.out.println(map.get("uid"));
8: System.out.println(map.get("bid"));
9: return "success";
10: }
11:
12: }
URL:
1: testPathVariable user 29 book 101
applicationContext.xml
1:
④. URI template 还支持正则表达式 。具体请看API
⑤. 使用@PathVariable可以让我们进行REST风格的编程,简单理解REST:对网络中某一资源的操作使用一个URI进行表示,然后使用状态来(GET、POST、PUT、DELETE)表示某种操作
原来对user进行CURD 使用了@PathVariable之后的CRUD
/user/get?id=10
/user/post?…
/user/update?id=10…
/user/delete?id=10
/user/id=10 RequestMethod.GET
/user/… RequestMethod.POST
/user/id=10… RequestMethod.PUT
/user/id=10 RequestMethod.DELETE
为了使普通表单支持PUT、DELETE请求,可以在POST请求下添加一个隐藏域(),然后在web.xml中配置HiddenHttpMethodFilter
对某用户的订单进行CRUD,代码:
1: @RequestMapping("/testPathVariable/user/{uid}")
2: @Controller
3: public class TestPathVariable {
4:
5: /*
6: *获取某用户的所有订单
7: * */
8: @RequestMapping(value = "/order", method = RequestMethod.GET)
9: public String testGET(@PathVariable Integer uid) {
10: System.out.println("GET: " + " user-" + uid);
11: return "success";
12: }
13:
14: /*
15: * 获取某用户的某个订单详情
16: * */
17: @RequestMapping(value = "/order/{oid}", method = RequestMethod.GET)
18: public String testGET_OID(@PathVariable Integer uid, @PathVariable Integer oid) {
19: System.out.println("GET_OID: " + " user-" + uid + " order-" + oid);
20: return "success";
21: }
22:
23: /*
24: * 修改某用户的某个订单的总价
25: * params = {"total"}
26: * 若加params,则请求中必须有该变量,没有会报错。
27: * 如果不加params,则请求中不强制要求包含该变量
28: * 不包含时,则parameter中的对应变量值为为Null,
29: * 如请求/testPathVariable/user/29/order/101
30: * 则total=null
31: * 包含,则parameter中的对应变量值为请求中的值
32: * 如请求/testPathVariable/user/29/order/101?total=1000
33: * 则total=1000
34: * */
35: @RequestMapping(value = "/order/{oid}", method = RequestMethod.PUT, params = {"total"})
36: public String testPUT(@PathVariable Integer uid, @PathVariable Integer oid, Integer total) {
37: System.out.println("PUT: " + " user-" + uid + " order-" + oid);
38: System.out.println(total);
39: return "success";
40: }
41:
42: /*
43: * 新增某用户的订单,变量略
44: * */
45: @RequestMapping(value = "/order", method = RequestMethod.POST)
46: public String testPOST_(@PathVariable Integer uid) {
47: System.out.println("POST: " + " user-" + uid + " order:订单信息");
48: return "success";
49: }
50:
51: /*
52: * 删除某用户的某个订单
53: * */
54: @RequestMapping(value = "/order/{oid}", method = RequestMethod.DELETE)
55: public String testDELETE(@PathVariable Integer uid, @PathVariable Integer oid) {
56: System.out.println("GET: " + " user-" + uid + " order-" + oid);
57: return "success";
58: }
59: }
index.jsp:
1:
2:
6:
7:
8:
12:
13:
14:
15:
20:
21:
22:
23:
24: 获取ID为29用户的所有订单
25:
26:
27:
28: 获取ID为29的用户,ID为101的订单的详情
29:
30:
31:
在web.xml中配置HiddenHttpMethodFilter:
1:
2: hiddenHttpMethodFilter
3: org.springframework.web.filter.HiddenHttpMethodFilter
4:
5:
6: hiddenHttpMethodFilter
7: /*
8: