這兩個參數都是用來處理request中的參數值的
但是處理的參數形式(url)不同
@RequestParam 處理的是 http://localhost/paper/editpage?paperId=1
@PathVariable 處理的是 http://localhost/paper/editpage/1
(我這裡吧端口号設定為80了,是以直接localhost就行了)
前端頁面代碼
<a class="btn btn-sm btn-primary"
th:href="@{/paper/editpage/} + ${paper.paperId}" target="_blank" rel="external nofollow" >編輯
<!--經過thymeleaf渲染之後: /paper/editpage/1 -->
</a>
<a class="btn btn-sm btn-primary"
th:href="@{/paper/editpage(paperId=${paper.paperId})}" target="_blank" rel="external nofollow" >測試連接配接
<!--經過thymeleaf渲染之後: /paper/editpage?paperId=1 -->
</a>
|
經過thymeleaf渲染之後
Controoler處理代碼
@GetMapping("/editpage/{paperId}")
public ModelAndView editPage(@PathVariable("paperId") Integer paperId) {
System.out.println("PathVariable:" + paperId);
Paper paper = paperService.selectPaper(paperId);
System.out.println(paper);
return new ModelAndView("paper/addPaper", "paper", paper);
}
@GetMapping("/editpage")
public ModelAndView editPagetest(@RequestParam("paperId") Integer paperId) {
System.out.println("RequestParam:" + paperId);
Paper paper = paperService.selectPaper(paperId);
System.out.println(paper);
return new ModelAndView("paper/addPaper", "paper", paper);
}
|
點選 編輯 按鈕之後
點選 測試連接配接 之後