【发布时间】:2019-09-17 21:20:48
【问题描述】:
我有一个很大的表单,我在数据库中保存了参数,因此下次用户进入时,之前的选择会在各种下拉列表中预先选择。
与此帖类似:Thymeleaf + Spring form - How load persisted select box values from controller to view?
主要区别在于我有大量参数,我正在尝试使其更通用。
基本上,我在 Java 中有一个 ParameterSet 对象,它带有一个 getParameterByName 函数和一个 contains 方法,用于检查是否在之前的运行中选择了下拉列表的动态可能值。
我的问题可能与 Thymeleaf 语法有关。我一直在玩 th:selected='${campaignRunCommand.parameterSet.parameters.getParameterByName("states").contains("${st}")}' 而且我什至不确定是否可以像这样在 Thymeleaf 中发送没有参数的静态值。
<select class="js-example-basic-multiple" style="width: 75%"
id="cstates" name="cstates" multiple="multiple">
<option th:each="st : ${states}" th:value="${st}" th:selected='${campaignRunCommand.parameterSet.parameters.getParameterByName("states").contains("${st}")}'
th:text="${st}"></option>
【问题讨论】:
-
使用spring的模型类从getParameterByName方法发送数据。见documentacion oficial