@PathVariable 可以来映射 URL 中的占位符到目标方法的参数中
先看页面部分:
jsp页面请求
<a href="springmvc/testPathVariable/1">Test PathVariable</a>
后台action部分:
Action中方法
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable: " + id);
return SUCCESS;
}
就是直接从@RequestMapping映射的路径里面把参数读取出来
thymeleaf语法里面的URL的传参问题:
<a th:href="@{/order/details(id=3)}">
就相当于:
<a href="/order/details?id=3">
还可以使用正常参数的路径变量的形式包含参数,但在URL的路径中指定一个占位符:
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">
上面输出结果为:
<a href="/order/3/details?action=show_all">