片段中的 Bean 预处理器没有正确处理我的表单对象

Bean preprocessor in fragment isn't processing my form object correctly

我一直在为我的应用开发一个灵活的分页解决方案,这里是片段代码:

<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>

我希望有人觉得这有用!