私はアプリの柔軟なページネーションソリューションに取り組んできました。フラグメントコードは次のとおりです。
<div th:fragment="pagination(form, postUrl)">
<nav aria-label="Page Navigation">
<ul class="pagination justify-content-center">
<li class="page-item"
th:classappend="${form?.page == 1} ? disabled : ''">
<form th:action="@{${postUrl}}" th:object="${__${form}__}"
method="post">
<input hidden th:field="${form?.page}" th:value="${form?.page} - 1" />
<button class="page-link" th:text="#{page.previous}" />
</form>
</li>
<div
th:with="pageLimit=${form?.totalPages > 0} ? ${form?.totalPages} : 1">
<li class="page-item"
th:each="i : ${#numbers.sequence(1, pageLimit)}">
<form th:action="@{${postUrl}}" th:object="${__${form}__}"
method="post">
<input hidden th:field="*{page}" th:value="${i}" />
<button class="page-link" style="border-radius: 0px"
th:text="${i}" />
</form>
</li>
</div>
<li class="page-item">
<form th:action="@{${postUrl}}" th:object="${__${form}__}"
method="post">
<input hidden th:field="*{page}" th:value="${form?.page} + 1" />
<button class="page-link" th:text="#{page.next}" />
</form>
</li>
</ul>
</nav>
</div>
そしてフラグメント変数はこのように渡されます:
<div
th:replace="fragments/pagination :: pagination(${objectForm}, '/my/url-is-here')">
ただし、ビューを処理すると、次のエラーが発生します(わかりやすくするために省略しています)。
Exception evaluating SpringEL expression: "my.object.ObjectForm@66bc4dd8" (template: "fragments/pagination" - line 6, col 37)
...
org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'bean_ref(@)'
これは、Beanの前処理で間違いなく間違いです。フラグメント__${form}__
やth:with
変数を使用せずにコードをそのまま使用すると、問題はありません。では、ここで何が起こっているのか誰か知っていますか?
どんな洞察もいただければ幸いです!
それで、私はそれについてさらに作業しましたが、Thymeleaf変数がどのように使用されているのかを誤解していることがわかりました。私はそれが何かと関係があると信じていth:field
ます。それを変更してname
問題を解決しました。
私の理解では、Thymeleaf変数はすでに処理されており、追加の処理を必要としない変数であるため、__ $ {form} __は機能しません。
さらに、私がこれに取り組んだとき、私は変数にformVariables
プレーンテキストをth
属性に渡すだけでは機能しないことがわかったので、最初に処理する必要があるため、プリプロセッサーになることがわかりました。
<div class="sticky-top paginator" th:fragment="pagination(form, postUrl, formVariables)">
<nav aria-label="Page Navigation">
<ul class="pagination justify-content-center">
<li class="page-item"
th:classappend="${form.page == 1} ? disabled : ''">
<form th:action="@{${postUrl}}" th:object="${form}" method="post">
<div th:include="__${formVariables}__"></div>
<input hidden name="page" th:value="${form.page - 1}" />
<button class="page-link" th:text="#{page.previous}" />
</form>
</li>
<th:block
th:with="pageLimit=${form.totalPages > 0} ? ${form?.totalPages} : 1">
<li th:each="i : ${#numbers.sequence(1, pageLimit)}"
class="page-item" th:classappend="${i == form.page} ? active : ''">
<form th:action="@{${postUrl}}" th:object="${form}" method="post">
<div th:include="__${formVariables}__"></div>
<input hidden name="page" th:value="${i}" />
<button class="page-link" style="border-radius: 0px"
th:text="${i}" />
</form>
</li>
</th:block>
<li class="page-item"
th:classappend="${form.page >= form.totalPages} ? disabled : ''">
<form th:action="@{${postUrl}}" th:object="${form}" method="post">
<div th:include="__${formVariables}__"></div>
<input hidden name="page" th:value="${form.page + 1}" />
<button class="page-link" th:text="#{page.next}" />
</form>
</li>
</ul>
</nav>
</div>
誰かがこれが便利だと思うことを願っています!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加