春天Thymeleaf传递地图到形式

问题描述 投票:0回答:1

我有一个属性hashmap,我将其传递给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:

@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()”工作,我不能发回更改的参数。

正如在official doc中提到的那样,我尝试使用这样的th:field方法,并使用Entry set进行循环:

<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>

我的所有尝试都失败并出现错误:

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)

我做错了什么?

spring forms post thymeleaf
1个回答
1
投票

据我所知,你不能使用Map作为你的表格对象。您需要一个具有Map作为其属性之一的对象。

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;
    }
}

完成后,更改控制器方法:

// 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应如下所示:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.