Spring MVC表单:select标签,多选未正确绑定?

发布于 2024-07-15 17:40:02 字数 935 浏览 9 评论 0原文

我正在尝试创建一个表单来编辑现有数据库行。 我正在使用 Spring MVC 表单标签自动将 html 绑定到表单支持对象。 该行与另一个表具有多对多关系,我尝试使用 form:select 标签用多选框来表示该关系;

<form:select path="rules">
    <form:options items="${bundle.rules}" itemValue="name" itemLabel="name"/>
</form:select>

我使用 Hibernate 进行持久化,因此关系在 Bundle pojo 中表示为 HashSet。

 private Set<Rule> rules = new HashSet<Rule>(0);

如果没有页面上的选择框,对象将正确更新到数据库,但是使用选择框,对象将不会更新到数据库,并且我在 log4j 日志中收到此错误,请注意此错误不会导致异常,仅在日志中可见;

DEBUG org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:256) - Data binding errors: 1

无论我取消选择选择框中的项目,都会发生这种情况,整个表单都拒绝正确提交。 谁能帮我?

我知道 如何绑定Spring MVC 中表单的集合属性,这与这个问题类似,不幸的是,没有一个建议对我的问题有用。

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;

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

 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;

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2024-07-22 17:40:02

问题出在您的表单提交上。 Spring 无法绑定命令的对象,因此它不会提交表单,而是将您重定向到 formView。

成功执行绑定后,您将看到以下消息:

No errors -> processing submit

要解决您的问题,您需要向控制器注册 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;
    }
  });
}

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.

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:

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;
    }
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文