【问题标题】:Returning values from Thymeleaf view从 Thymeleaf 视图返回值
【发布时间】:2016-04-27 13:27:19
【问题描述】:

我正在使用 Thymeleaf 和 Spring 4 开发应用程序。我需要在选择后返回一个值。

<form class="form-horizontal" th:action="@{/processInfoBook}"   th:object="${relationshipDTO}" method="post" enctype="multipart/form-data">
<select style="visibility:hidden" hidden="hidden" class="form-control" th:field="*{typeTo}">
    <option th:each="book : ${session.Book}" th:value="${book.getClass().getSimpleName()}"></option>
</select>

通过这种方法,我的控制器中的变量 String typeTo 已设置,但使用此方法,它不是。

<input th:with="bookType=${session.Book[1]}" type="hidden" th:field="*{typeTo}" th:value="${bookType.getClass().getSimpleName()}" />

谁能解释一下这里发生了什么以及如何修复它,我更喜欢使用第二种方法,因为我有另一个选择,我复制相同的代码只是为了设置另一个变量。

谢谢!

public class RelationshipDTO {

    private String typeFrom;
    private String typeTo;
    private String dataFrom;
    private String bookTo;
    ...setters and getters

}

还有观点:

<input type="hidden" th:field="${relationshipDTO.dataFrom}" />
<input type="hidden" th:field="${relationshipDTO.typeFrom}" />
<div class="form-group">
<label for="anotherBook" class="col-sm-5 control-label">TO</label> 
<select class="form-control" th:field="${relationshipDTO.bookTo}">
    <option th:each="book : ${session.Book}" th:value="${book.name}" th:text="${book.name}"></option>
</select> 
//This is the null field
<input th:with="bookType=${session.Book[1]}" type="hidden" th:field="${relationshipDTO.typeTo('hola')}"/>
                    </div>

【问题讨论】:

    标签: spring thymeleaf


    【解决方案1】:

    我认为您是在混合概念,如果您对 thymeleaf 说您将从表单和特定对象呈现值,那么您必须使用 th:field 属性但 th:value。

    另外你的标签有错别字

    th:with="bookType=${session.Book[1]}"

    您在输入中分配给一个请求变量“bookType”。

    只用这个试试:

    <input th:with="${session.Book[1]}" type="hidden" th:field="*{typeTo}"/>
    

    我假设属性 typeTo 在你的对象关系 DTO 中。

    【讨论】:

    • 我已经尝试过了,但是我的属性仍然为空。 Thymeleaf 怎么知道我试图将哪些数据发送到字段 typeTo?
    • 如果是原始或标准 java 对象,spring 将绑定您的字段值以转换为字符串、整数、...如果是您为业务逻辑创建的自定义对象或枚举, 你需要设置你的对象的 id 或者如果是一个枚举它的名字。请让我知道你的实体是什么样子,我会尽力给你一个很好的例子
    • 实际上它并不是一个实体。从一个文件中,我将元素上传到会话中,并将它们放入列表中。
    • 你能用这个实体更新你的问题吗?关系DTO
    • @sirandy 现在试试这个例子,也请复制 for 标签,我很困惑你是如何使用 th:field 属性的
    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 2015-12-30
    • 2019-05-22
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多