Thymeleaf使用请求参数比较/计算

问题描述: Thymeleaf模板里想要调用url上的参数

url:http://localhost:8081/index?pageNo=0&size=15&keyword=%E6%B4%97%E8%A1%A3%E6%9C%BA&sort=price

下面展示如何使用sort参数:

代码:

<div class="sort" th:with="text=${param.sort==null?null:param.sort[0]}">
    <a th:href="@{'/index?pageNo=0&size=15&keyword='+${keyword}}" title="按默认排序"
       th:classappend="${text == null}?'on':''">默认</a>
    <a th:href="@{'/index?pageNo=0&size=15&keyword='+${keyword}+'&sort=discussCount'}"
       title="按评论数从高到低排序" class=""
       th:classappend="${  text eq 'discussCount'}?'on':''"
       >评论数</a>
    <a th:href="@{'/index?pageNo=0&size=15&keyword='+${keyword}+'&sort=price'}"
       title="按价格从低到高排序" class=""
       th:classappend="${  text eq 'price'}?'on':''"
       >
        价格</a>
</div>

第一步:在父容器上定义局部变量接受指定参数值

th:with="text=${param.sort==null?null:param.sort[0]}"

局部变量:text 

指定参数:param.sort[0]

注意这里使用三元运算符处理了空值,避免了空指针异常,且参数的值类型是数据需要用索引

 

第二步 使用 局部变量:text 

th:classappend="${  text eq 'price'}?'on':''"

这里我用其值控制 class

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章