春季Thymeleaf传递地图形成 [英] Spring Thymeleaf pass map to form
问题描述
我有一个Properties哈希图,该哈希图在settings.html中传递给表单:
I have a Properties hashmap, that i pass to my form in settings.html:
<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>
一般的想法是显示可以保存并传递回控制器SettingsController的属性:
The general idea is to show the properties, that can be saved and passed back to controller SettingsController:
@Controller
@RequestMapping(value = "/settings")
public class SettingsController {
@Autowired
SettingsService settingsService;
@Value("${postgres-bin-dir}")
private String pgDirectory;
@GetMapping(value = "/current")
public String currentSettings(Model model) {
Map<String, String> propertiesMap = settingsService.getSettingsMap();
model.addAttribute("pgpath", pgDirectory);
model.addAttribute("properties", propertiesMap);
model.addAttribute("title", String.format("Current PgBackupper'a settings(%d)", propertiesMap.size()));
return "settings/settings";
}
@PostMapping(value = "/modify", params = "action=save")
public String changeProperties(
@RequestParam(value = "properties") Map<String, String> properties,
RedirectAttributes redirectAttributes){
redirectAttributes.addFlashAttribute("message","Settings changed succesfully");
return "redirect:/settings/current";
}
}
所以我想让我的post方法"modifyProperties()"工作,但我无法将已更改的参数发送回去.
So I want to get my post method "modifyProperties()" work, bunt i can't send changed parameters back.
官方文档 ,我尝试使用类似th:field的方法,并使用Entry set进行循环:
As it is mentionet at official doc, I've tried to use such a th:field method, like, and used Entry set for looping:
<div th:each="property : ${properties.entrySet()}">
<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}"
th:field="*{property.get(${property.key})}" />
</div>
</div>
我所有的尝试都失败,并显示一个错误:
All my attempts fail with an error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Mar 04 14:07:42 MSK 2018
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor
'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (settings/settings:29)
我该怎么办?
推荐答案
据我所知,您不能使用Map
作为表单对象.您需要具有一个以Map
作为其属性之一的对象.
As far as I know, you can't use a Map
as your form object. You need to have an object that has a Map
as one of its properties.
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;
}
}
完成此操作并更改了控制器方法:
Once you've done that, and changed your controller methods:
// 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) {
表单的html看起来应该像这样:
The html for the form should look something like this:
<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>
这篇关于春季Thymeleaf传递地图形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!