SpringMVC(三)@PathVariable
使用@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: <a href="testPathVariable/user/29">testPathVariable user 29</a>
②. 一个方法可以有多个@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: <a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>
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: <a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>
③. @PathVariable 还可以使用在 map 参数上,但是必须配置<mvc:annotation-driven />
代码:
1: @RequestMapping("/testPathVariable/user/{uid}")
2: @Controller
3: public class TestPathVariable {
4:
5: @RequestMapping("/book/{bid}")
6: public String testMultiplePathVariable_Map(@PathVariable Map<String, String> map) {
7: System.out.println(map.get("uid"));
8: System.out.println(map.get("bid"));
9: return "success";
10: }
11:
12: }URL:
1: <a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>applicationContext.xml
1: <mvc:annotation-driven></mvc:annotation-driven>
④. 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请求下添加一个隐藏域(<input type="hidden" name="_method" value="PUT、DELETE"/>),然后在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: <form action="testPathVariable/user/29/order/101" method="post">
3: <input type="hidden" name="_method" value="DELETE"/><br/>
4: <input type="submit" value="删除ID为29用户的ID为101的订单"/>
5: </form>
6:
7: <br/><br/>
8: <form action="testPathVariable/user/29/order" method="post">
9: 输入订单信息....<br/>
10: <input type="submit" value="ID为29用户新增订单"/>
11: </form>
12:
13: <br/><br/>
14:
15: <form action="testPathVariable/user/29/order/101" method="post">
16: <input type="hidden" name="_method" value="put"/><br/>
17: <input type="text" name="total"/><br/>
18: <input type="submit" value="修改ID为29用户,ID为101订单的总价"/>
19: </form>
20:
21: <br/><br/>
22:
23:
24: <a href="testPathVariable/user/29/order">获取ID为29用户的所有订单</a>
25:
26: <br/><br/>
27:
28: <a href="testPathVariable/user/29/order/101">获取ID为29的用户,ID为101的订单的详情</a>
29:
30: <br/><br/>
31:
在web.xml中配置HiddenHttpMethodFilter:
1: <filter>
2: <filter-name>hiddenHttpMethodFilter</filter-name>
3: <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
4: </filter>
5: <filter-mapping>
6: <filter-name>hiddenHttpMethodFilter</filter-name>
7: <url-pattern>/*</url-pattern>
8: </filter-mapping>
SpringMVC(三)@PathVariable的更多相关文章
- SpringMVC 三种异常处理方式
SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...
- SpringMVC(三):@RequestMapping中的URL中设定通配符,可以使用@PathVariable映射URL绑定的占位符
1)带占位符的URL是Spring3.0新增的功能,该功能在SpringMVC向REST目标挺进发展过程中具有里程碑的意义. 2)通过@PathVariable可以将URL中占位符参数绑定到控制器处理 ...
- springmvc中@PathVariable和@RequestParam的区别
顾名思义, @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数. 我的url; http://localhost:808 ...
- springmvc中@PathVariable和@RequestParam的区别(百度收集)
http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNow=2 你可以把这地址分开理解,其中问号前半部分:http://lo ...
- springmvc(三) 参数绑定、
前面两章就介绍了什么是springmvc,springmvc的框架原理,并且会简单的使用springmvc以及ssm的整合,从这一章节来看,就开始讲解springmvc的各种功能实现,慢慢消化 --W ...
- SpringMVC(三):参数绑定、输入输出转换
一.参数解析绑定 1. 自定义绑定:不绑定某些项 @InitBinder private void initBinder(WebDataBinder dataBinder) { dataBinder. ...
- SpringMVC中@pathVariable和@RequestParam注解的区别
@pathVariable和@RequestParam的区别 @pathVariable:是从路径中获取变量,也就是把路径当做变量 @RequestParam:是从请求里面获取参数 案例分析: /Sp ...
- SpringMVC(三) RESTful架构和文件上传下载
RESTful架构 REST全名为:Representational State Transfer.资源表现层状态转化.是目前最流行的一种互联网软件架构. 它结构清晰.符合标准.易于理解.扩展方便,所 ...
- SpringMvc中@PathVariable注解简单的用法
@PathVariable /** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中. * @param id * @return */ jsp页面请求 <a h ...
- SpringMVC(三) RequestMapping修饰类
SpringMVC使用@RequestMapping 注解为控制器指定可以处理哪些URL请求. 可以用于类定义以及方法定义: 类定义:提供初步的请求映射信息.相对于WEB应用的根目录. 方法处:提供进 ...
随机推荐
- 洛谷P1101 单词方阵【DFS】
给一n \times nn×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 88 个方向的任一方向,同一单词摆放时不再改变方向 ...
- python-flask-1
https://askubuntu.com/questions/244641/how-to-set-up-and-use-a-virtual-python-environment-in-ubuntu ...
- Python语言简介
一.Python语言发展史 1989年吉多·范罗苏姆(Guido van Rossum)中文外号“龟叔”,圣诞节期间开始编写Python语言的编译器. Python这个名字,来自Guido所挚爱的电视 ...
- Python语言数据结构和语言结构(2)
目录 1. Python预备基础 2. Python数据类型 3. Python条件语句 4. while循环和for循环 1. Python预备基础 1.1 变量的命名 变量命名规则主要有以下几 ...
- 4.Thymeleaf的常用标签
一.常用标签 二.foreach案例 1.创建项目 2. 创建Student.java package cn.kgc.pojo; /** * Created by Administrator on 2 ...
- java中类的路径为什么这么长
- 简陋版:基于python的自动化测试框架开发
项目背景: XXXX银行项目采用的是B/S架构,主要是为了解决银行业务中的柜员.凭证.现金.账务等来自存款.贷款.会计模块的管理. 手工弊端: 1.项目业务复杂度高,回归测试工作量大2.单个接口功能比 ...
- Error解决:Property's synthesized getter follows Cocoa naming convention for returning 'owned'
在项目中定义了以new开头的textField.结果报错: 先看我的源代码: #import <UIKit/UIKit.h> @interface ResetPasswordViewCon ...
- luogu1955 [NOI2015] 程序自动分析
题目大意 假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量相等/不等的约束条件,请判定是否可以分别为每一个变量赋予恰当的值,使得上述所有约束条件同时被满足.i, ...
- codetemplate
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...