无法将复选框绑定到@ModelAttribute。

HTML:

<form method="post">
    <p><input type="checkbox" name="list" value="v1" /></p>
    <p><input type="checkbox" name="list" value="v2" /></p>
    <p><input type="checkbox" name="list" value="v3" /></p>
    <p><input type="checkbox" name="list" value="v4" /></p>
    <p><input type="submit" value="Send" /></p>
</form>


Java:

@RequestMapping(value = "/", method = RequestMethod.POST)
public String index(@ModelAttribute("list")List<String> list, ModelMap model)
{
    LoggerFactory.getLogger(this.getClass()).debug("list: " + list.size() + " " + list);
    ...
}


检查v2和v3。仅在“索引”第一个值中:

清单:1 [v2]

如果我使用spring标签创建表单:

<form:form modelAttribute="testForm">
    <p><form:checkbox path="list" value="v1" /></p>
    <p><form:checkbox path="list" value="v2" /></p>
    <p><form:checkbox path="list" value="v3" /></p>
    <p><form:checkbox path="list" value="v4" /></p>
    <p><input type="submit" value="Отправить" /></p>
</form:form>


TestForm.java:

public class TestForm
{
    private String[] list;

    public String[] getList()
    {
        return list;
    }

    public void setList(String[] list)
    {
        this.list = list;
    }
}


一切都好!

如何在没有spring标签的情况下将复选框绑定到@ModelAttribute?

最佳答案

检查:public String index(@RequestParam("list") List<String> list, ModelMap model)

关于java - Spring 。将复选框绑定(bind)到ModelAttribute,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9031323/

10-09 12:37