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";
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值