【发布时间】: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>
【问题讨论】: