SpringMVC(三)@PathVariable
2015-07-06 13:08
483 查看
使用@PathVariable可以快速的访问,URL中的部分内容。
①. 在@RequestMapping的value中使用URI template({变量名}),然后在@RequestMapping注解方法的需要绑定的参数前,使用@PathVariable指定变量名(如果变量名和参数名一致也可以不指定),从而将URL中的值绑定到参数上。
代码:
URL:
②. 一个方法可以有多个@PathVriable注解
URI template 可以这样,全部在方法上
代码:
URL:
URI template还可以这样, 加在类和方法上
代码:
URL:
③. @PathVariable 还可以使用在 map 参数上,但是必须配置<mvc:annotation-driven />
代码:
URL:
applicationContext.xml
④. URI template 还支持正则表达式 。具体请看API
⑤. 使用@PathVariable可以让我们进行REST风格的编程,简单理解REST:对网络中某一资源的操作使用一个URI进行表示,然后使用状态来(GET、POST、PUT、DELETE)表示某种操作
为了使普通表单支持PUT、DELETE请求,可以在POST请求下添加一个隐藏域(<input type="hidden" name="_method" value="PUT、DELETE"/>),然后在web.xml中配置HiddenHttpMethodFilter
对某用户的订单进行CRUD,代码:
index.jsp:
在web.xml中配置HiddenHttpMethodFilter:
①. 在@RequestMapping的value中使用URI template({变量名}),然后在@RequestMapping注解方法的需要绑定的参数前,使用@PathVariable指定变量名(如果变量名和参数名一致也可以不指定),从而将URL中的值绑定到参数上。
代码:
@RequestMapping("/testPathVariable")
@Controller
public class TestPathVariable {
/*
* URI模板指定了一个变量名为id的变量,当控制器处理请求时会将 id 替换为正确的值
*
* 若请求为 testPathVariable/user/29,则uid=29,输出29
*
* */
@RequestMapping("/user/{id}")
public String testPathVariable(@PathVariable("id") Integer uid) {
System.out.println(uid);
return "success";
}
}
URL:
<a href="testPathVariable/user/29">testPathVariable user 29</a>
②. 一个方法可以有多个@PathVriable注解
URI template 可以这样,全部在方法上
代码:
@RequestMapping("/testPathVariable")
@Controller
public class TestPathVariable {
@RequestMapping("/user/{uid}/book/{bid}")
public String testMultiplePathVariable(@PathVariable("uid") Integer uid,@PathVariable("bid") Integer bid) {
System.out.println(uid);
System.out.println(bid);
return "success";
}
}
URL:
<a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>
URI template还可以这样, 加在类和方法上
代码:
@RequestMapping("/testPathVariable/user/{uid}")
@Controller
public class TestPathVariable {
@RequestMapping("/book/{bid}")
public String testMultiplePathVariable(@PathVariable("uid") Integer uid, @PathVariable("bid") Integer bid) {
System.out.println(uid);
System.out.println(bid);
return "success";
}
}
URL:
<a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>
③. @PathVariable 还可以使用在 map 参数上,但是必须配置<mvc:annotation-driven />
代码:
@RequestMapping("/testPathVariable/user/{uid}")
@Controller
public class TestPathVariable {
@RequestMapping("/book/{bid}")
public String testMultiplePathVariable_Map(@PathVariable Map<String, String> map) {
System.out.println(map.get("uid"));
System.out.println(map.get("bid"));
return "success";
}
}
URL:
<a href="testPathVariable/user/29/book/101">testPathVariable user 29 book 101</a>
applicationContext.xml
<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=10RequestMethod.GET /user/… RequestMethod.POST /user/id=10…RequestMethod.PUT /user/id=10 RequestMethod.DELETE |
对某用户的订单进行CRUD,代码:
@RequestMapping("/testPathVariable/user/{uid}")
@Controller
public class TestPathVariable {
/*
*获取某用户的所有订单
* */
@RequestMapping(value = "/order", method = RequestMethod.GET)
public String testGET(@PathVariable Integer uid) {
System.out.println("GET: " + " user-" + uid);
return "success";
}
/*
* 获取某用户的某个订单详情
* */
@RequestMapping(value = "/order/{oid}", method = RequestMethod.GET)
public String testGET_OID(@PathVariable Integer uid, @PathVariable Integer oid) {
System.out.println("GET_OID: " + " user-" + uid + " order-" + oid);
return "success";
}
/*
* 修改某用户的某个订单的总价
* params = {"total"}
* 若加params,则请求中必须有该变量,没有会报错。
* 如果不加params,则请求中不强制要求包含该变量
* 不包含时,则parameter中的对应变量值为为Null,
* 如请求/testPathVariable/user/29/order/101
* 则total=null
* 包含,则parameter中的对应变量值为请求中的值
* 如请求/testPathVariable/user/29/order/101?total=1000
* 则total=1000
* */
@RequestMapping(value = "/order/{oid}", method = RequestMethod.PUT, params = {"total"})
public String testPUT(@PathVariable Integer uid, @PathVariable Integer oid, Integer total) {
System.out.println("PUT: " + " user-" + uid + " order-" + oid);
System.out.println(total);
return "success";
}
/*
* 新增某用户的订单,变量略
* */
@RequestMapping(value = "/order", method = RequestMethod.POST)
public String testPOST_(@PathVariable Integer uid) {
System.out.println("POST: " + " user-" + uid + " order:订单信息");
return "success";
}
/*
* 删除某用户的某个订单
* */
@RequestMapping(value = "/order/{oid}", method = RequestMethod.DELETE)
public String testDELETE(@PathVariable Integer uid, @PathVariable Integer oid) {
System.out.println("GET: " + " user-" + uid + " order-" + oid);
return "success";
}
}
index.jsp:
<form action="testPathVariable/user/29/order/101" method="post">
<input type="hidden" name="_method" value="DELETE"/><br/>
<input type="submit" value="删除ID为29用户的ID为101的订单"/>
</form>
<br/><br/>
<form action="testPathVariable/user/29/order" method="post">
输入订单信息....<br/>
<input type="submit" value="ID为29用户新增订单"/>
</form>
<br/><br/>
<form action="testPathVariable/user/29/order/101" method="post">
<input type="hidden" name="_method" value="put"/><br/>
<input type="text" name="total"/><br/>
<input type="submit" value="修改ID为29用户,ID为101订单的总价"/>
</form>
<br/><br/>
<a href="testPathVariable/user/29/order">获取ID为29用户的所有订单</a>
<br/><br/>
<a href="testPathVariable/user/29/order/101">获取ID为29的用户,ID为101的订单的详情</a>
<br/><br/>
在web.xml中配置HiddenHttpMethodFilter:
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
相关文章推荐
- Java的SPI机制
- spring xml properties split with comma for list
- 《JAVA程序设计》实训第四天——《猜猜看》游戏
- Spring学习(6)---Bean定义及作用域的例子
- springMVC学习笔记-视图传值给Controllerb
- spring mvc controller间跳转 重定向 传参
- java框架技术之hibernate核心类和接口
- java框架技术之hibernate运行原理
- 什么是线程安全和线程不安全
- JAVA程序设计》实训第三天——《猜猜看》游戏
- Eclipse Android项目导入Android Studio时常见的编译错误及解决办法
- 《JAVA程序设计》实训第二天——《猜猜看》游戏
- eclipse错误 Exception raised during rendering:java.lang.System.arraycopy([CI[CII)V
- 下载文档--Struts2中国的文件下载被显示为空间的问题
- eclipse错误 timeout
- [LeetCode][Java] String to Integer (atoi)
- Spring MVC 之Transaction rollback
- 用Java实现AES加密
- EJB到底为何物?
- Java Web验证码