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

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