Spring Thymeleaf pass映射到表单
我有一个属性hashmap,在settings.html中传递给我的表单:Spring Thymeleaf pass映射到表单,spring,forms,post,thymeleaf,Spring,Forms,Post,Thymeleaf,我有一个属性hashmap,在settings.html中传递给我的表单: <form action="#" th:action="@{/settings/modify}" method="post" th:object="${properties}"> <div th:each="property : ${properties}"> <div class="form-group"> <la
<form action="#" th:action="@{/settings/modify}" method="post" th:object="${properties}">
<div th:each="property : ${properties}">
<div class="form-group">
<label th:for="${property.key}" name="key" th:text="${property.key}">Property</label>
<input type="text"
class="form-control"
name="value"
th:id="${property.key}"
th:value="${property.value}" />
</div>
</div>
<button type="submit" class="btn btn-default" name="action" value="save">Submit</button>
<button type="reset" class="btn btn-default" name="action" value="reload">Reset</button>
</form>
我做错了什么?据我所知,您不能使用
映射作为表单对象。您需要有一个对象,该对象的属性之一是映射
public class PropertiesForm {
private Map<String, String> properties = new HashMap<>();
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}
及
表单的html应如下所示:
<form th:object="${form}" method="post">
<div th:each="property : ${form.properties.entrySet()}">
<div class="form-group">
<label th:for="*{properties['__${property.key}__']}" th:text="${property.key}">Property</label>
<input type="text" class="form-control" th:field="*{properties['__${property.key}__']}" />
</div>
</div>
</form>
财产
据我所知,您不能将地图
用作表单对象。您需要有一个对象,该对象的属性之一是映射
public class PropertiesForm {
private Map<String, String> properties = new HashMap<>();
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}
及
表单的html应如下所示:
<form th:object="${form}" method="post">
<div th:each="property : ${form.properties.entrySet()}">
<div class="form-group">
<label th:for="*{properties['__${property.key}__']}" th:text="${property.key}">Property</label>
<input type="text" class="form-control" th:field="*{properties['__${property.key}__']}" />
</div>
</div>
</form>
财产
// public String currentSettings(Model model) {
PropertiesForm form = new PropertiesForm();
form.setProperties(settingsService.getSettingsMap());
model.addAttribute("form", form);
public String changeProperties(@ModelAttribute("form") PropertiesForm form, RedirectAttributes redirectAttributes) {
<form th:object="${form}" method="post">
<div th:each="property : ${form.properties.entrySet()}">
<div class="form-group">
<label th:for="*{properties['__${property.key}__']}" th:text="${property.key}">Property</label>
<input type="text" class="form-control" th:field="*{properties['__${property.key}__']}" />
</div>
</div>
</form>