使用@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>

最新文章

  1. 详细介绍Mysql各种存储引擎的特性以及如何选择存储引擎
  2. 基于Erlang VM的函数式编程语言Elixir
  3. 使用Notepad++编码编译时报错(已解决?)
  4. 以Python角度学习Javascript(二)之DOM
  5. JS中的call()和apply()方法和bind()
  6. openstack libtray
  7. java中基于TaskEngine类封装实现定时任务
  8. SSIS如何引用外部DLL
  9. bootstrap学习笔记之基础导航条 http://www.imooc.com/code/3111
  10. Oracle查询优化改写--------------------范围处理
  11. java微信获取经纬度转换为高德坐标小结
  12. Java属性中指定Json的属性名称
  13. javaScript遍历对象、数组总结(转载)
  14. linux 安装python 和pip
  15. linux之cp命令(转载)
  16. 关于ELK
  17. CRM 2013 批量更新two options的缺省值
  18. MVC基于角色权限控制--菜单展示
  19. netty为啥要二次开发
  20. Android MVP模式就是这么回事儿

热门文章

  1. linux 中,mysql数据库备份操作
  2. Problem 25
  3. COOKIE, SESSION, JSESSION
  4. QT中tableview不能更新数据,why?
  5. POJ 1694 An Old Stone Game
  6. Visible Trees
  7. Future和Callable的使用
  8. git merge和git rebase的区别和异同
  9. Kafka中文文档学习笔记
  10. POJ 3710