1)帶占位符的URL是Spring3.0新增的功能,該功能在SpringMVC向REST目標挺進發展過程中具有里程碑的意義。
2)通過@PathVariable可以將URL中占位符參數綁定到控制器處理方法的入參中:URL中的{xxx}占位符可以通過@PathVariable("xxx")綁定到操作方法的入參中。
在HelloWord.java中添加testPathVariable方法:
package com.dx.springlearn.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("class_requestmapping") public class HelloWord { private static String SUCCESS = "success"; @RequestMapping("/testPathVariable/{id}") public String testPathVariable(@PathVariable(value = "id", required = true) Integer id) { System.out.println("testPathVariable: id: " + id); return SUCCESS; } }
index.jsp添加鏈接:
<a href="class_requestmapping/testPathVariable/1">testPathVariable</a> <br>