【问题标题】:Bean preprocessor in fragment isn't processing my form object correctly片段中的 Bean 预处理器未正确处理我的表单对象
【发布时间】:2020-06-10 03:29:24
【问题描述】:

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

<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 变量,它就可以了。那么有人知道这里发生了什么吗?

任何见解将不胜感激!

【问题讨论】:

  • 我有完全相同的错误。我通过切换到 th:name / th:value 暂时解决了。 th:name 可能被误用,例如列表如下: th:name="${'listInForm[' + ${i.index} + '].attributeName'}" 不好,但工作
  • 我想我暂时也可以解决这个问题,但我仍然想找到错误的根源。谢谢!

标签: html spring thymeleaf spring-el


【解决方案1】:

所以我又做了一些工作,结果发现我误解了 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>

我希望有人觉得这很有用!

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 2015-09-12
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    相关资源
    最近更新 更多