spring boot + thymeleaf:a标签 th:href使用

th:href

实现点击标题跳转 到 /productioin/id 查看问题详情

1----th:href="@{ ${} }形式

index.html 首页中展示商品的标题href
<!--     produce  /prodction/1    -->
<h3><a th:href="@{
           
    /prodction/+${production.id}}" th:text="${production.title}">同济七版上下册</a></h3>
Controller中 productionController
@GetMapping("/production/{proid}")
public String product(@PathVariable(name = "proid")Long proid,
                          Model model){
          
   
		/*传递html中 ${}中的值:PathVariable 来取GetMapping中{proid}的值*/                      
 }

效果1:

2----th:href="@{ (proId=${}) }形式形式

括号是为了传值用name接受

index.html
<h3><a th:href="@{/production(proId=${production.id})
th:text="${production.title}">xxx</a></h3>
controller
@GetMapping("/production")
    public String product(@RequestParam(name = "proId")Long proid,
                          Model model){
          
   
        /* @RequestParam 来接受?后的参数 */
        return "product";
      }

效果2: 举例: 比如在跳转第一页时:(分页)

<a th:href="@{/(page=1,search=${session.search},tag=${tag})}">
@GetMapping("/")
    public String index(HttpServletRequest request,
                        Model model,
                        @RequestParam(name = "page", defaultValue = "1") Integer page,
                        @RequestParam(name = "size", defaultValue = "5") Integer size,
                        @RequestParam(name = "search", required = false) String search,
                        @RequestParam(name = "tag", required = false) String tag) {
          
   
        PaginationDTO pagination = questionService.allQuestion(page, size, search, tag);
        List<String> hottags = hotTagCache.getHots();
        model.addAttribute("pagination", pagination);
        model.addAttribute("hottags", hottags);
        model.addAttribute("tag", tag);
        request.getSession().setAttribute("search", search);
        return "index";
    }
经验分享 程序员 微信小程序 职场和发展