You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thymeleaf表达式解析错误:分页模板代码报错求助

Fixing Thymeleaf's "Error parsing expression" in Pagination Template

Hey there, let's sort out that Thymeleaf parsing error you're facing with your pagination code. The root cause is how you're embedding Thymeleaf variables into the JavaScript function call in your th:href attribute—using backticks `${previous}` doesn't play nice with Thymeleaf's expression parser.

Here's the corrected code with two solid approaches:

Approach 1: String concatenation with Thymeleaf variable substitution

Wrap the Thymeleaf variable in single quotes (since the th:href attribute uses double quotes) to properly pass the value to your movePage function:

<ul class="results_perpage" >
  <li th:if="${previous != null}">
    <a th:href="javascript:movePage('${previous}');" class="results_menu" th:text="PREVIOUS"></a>
  </li>
  <li><a href="#" class="results_menu" th:text="${numPage}"></a></li>
  <li>
    <a th:href="javascript:movePage('${next}');" class="results_menu" th:text="NEXT"></a>
  </li>
</ul>

Approach 2: Use Thymeleaf inline expressions (cleaner syntax)

Thymeleaf's inline expression syntax [[${variable}]] is designed for embedding values into text/script contexts, making this easier to read and less error-prone:

<ul class="results_perpage" >
  <li th:if="${previous != null}">
    <a th:href="javascript:movePage('[[${previous}]]')" class="results_menu" th:text="PREVIOUS"></a>
  </li>
  <li><a href="#" class="results_menu" th:text="${numPage}"></a></li>
  <li>
    <a th:href="javascript:movePage('[[${next}]]')" class="results_menu" th:text="NEXT"></a>
  </li>
</ul>

Note: If previous/next are numeric values, you can omit the single quotes around the inline expression.

Quick additional checks:

  • Make sure your movePage() JavaScript function is defined somewhere in your page (Thymeleaf parsing success doesn't guarantee the function exists!).
  • Verify that previous, numPage, and next are correctly populated in your backend Model (you already handle previous being null with th:if, which is good!).

内容的提问来源于stack exchange,提问作者Salvador Borés

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:34:15