Spring MVC 表单:select Tag,多选未正确绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/706660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC form:select Tag, multiple selections not binding correctly?
提问by James McMahon
I am trying to create a form to edit an existing database row. I am using the Spring MVC form tag to auto bind the html to a form backing object. The row has a many to many relationship with another table, which I am trying to represent with a multiple select box using the form:select tag;
我正在尝试创建一个表单来编辑现有的数据库行。我正在使用 Spring MVC 表单标签将 html 自动绑定到表单支持对象。该行与另一个表有多对多的关系,我试图使用 form:select 标签用多选框表示;
<form:select path="rules">
<form:options items="${bundle.rules}" itemValue="name" itemLabel="name"/>
</form:select>
I am using Hibernate for persistence so the relationship is represent as a HashSet inside the Bundle pojo.
我使用 Hibernate 进行持久化,因此该关系表示为 Bundle pojo 中的 HashSet。
private Set<Rule> rules = new HashSet<Rule>(0);
Without the selection box on the page, the object will update to the database correctly, however with the selection box the object will not update to the database and I am getting this error in my log4j log, note that this error is not causing an exception, it is only visible in the logs;
如果页面上没有选择框,对象将正确更新到数据库,但是有了选择框,对象将不会更新到数据库,并且我在 log4j 日志中收到此错误,请注意此错误不会导致异常,仅在日志中可见;
DEBUG org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:256) - Data binding errors: 1
This happens regardless of wither I deselect items inside the select box, the entire form refuses to submit correctly. Can anyone help me?
无论我在选择框中取消选择项目,都会发生这种情况,整个表单拒绝正确提交。谁能帮我?
I am aware of How do I bind collection attributes to a form in Spring MVC, which is similar to this question, unfortunately none of the suggestions seemed useful to my problem.
我知道如何将集合属性绑定到 Spring MVC 中的表单,这类似于这个问题,不幸的是,这些建议似乎对我的问题没有用。
回答by kgiannakakis
The problem is with the submission of your form. Spring isn't able to bind an object of the command, so it doesn't submit the form, but redirects you to the formView instead.
问题在于提交表单。Spring 无法绑定命令的对象,因此它不会提交表单,而是将您重定向到 formView。
When the binding is successfully performed, you will see this message instead:
成功执行绑定后,您将看到此消息:
No errors -> processing submit
To solve your problem, you will need to register a CustomCollectionEditor with your controller. (See this link). It would be something like this:
要解决您的问题,您需要向控制器注册一个 CustomCollectionEditor。(请参阅此链接)。它会是这样的:
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
binder.registerCustomEditor(Set.class, "rules", new CustomCollectionEditor(Set.class)
{
protected Object convertElement(Object element)
{
String name = "";
if (element instanceof String)
name = (String) element;
return name != null ? new Rule(name) : null;
}
});
}

