【问题标题】:Spring MVC multiple rows form submit submits new ModelAttributeSpring MVC多行表单提交提交新的ModelAttribute
【发布时间】:2013-05-19 13:15:06
【问题描述】:

好的,它是 spring 的另一个 null ModelAttribute,但我已经尝试了我找到的所有东西,但它不起作用。

我已将本教程功能添加到我的网络应用程序 spring-mvc 中,一切都很好,除非我必须将数据从表单返回到我的 bean。

我用了两种post方法

@RequestMapping(method = RequestMethod.POST)
    public String openEdit(@RequestParam("notfound") String[] notFound,
                           Model model){
        EditForm editForm = editModel.createEditForm(notFound);
        model.addAttribute("editForm",editForm);
        return "Edit/EditOwners";
    }

    @RequestMapping(method = RequestMethod.POST,value = "/saveOwners")
    public String saveOwners(@ModelAttribute("editForm") EditForm editForm){
        editModel.addOwners(editForm);
        return "index";
    }

第一个工作正常,我可以在我的 Jsp 中看到数据,第二个得到一个新的 ModelAttribute。

这是我的表格

<form:form method="POST" action="saveOwners" modelAttribute="editForm" >
            <table class="table table-bordered table-striped table-hover table-condensed">
                <caption>
                    <h4>Edit The Owners</h4>
                </caption>
                <thead>
                <tr>
                    <td>Name</td>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${editForm.owners}" var="item" varStatus="status">
                    <tr>
                        <td>${item.name}</td>
                        <td><input type="text" name="${owners[status.index].outId}" value="${item.outId}" /> </td>
                        <td><input type="text" name="${owners[status.index].type}" value="${item.type}" /> </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <input type="submit" value="Save" class="btn btn-primary"/>
        </form:form>

这里似乎有什么问题? PS 我尝试了有无类型属性的输入,但它仍然为空,正如我所说,我可以看到我的数据,但我无法提交它们。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    您是否尝试将以下注释添加到控制器类的顶部?

    @SessionAttributes({ "editForm" })
    public class YourController {
      ...
    }
    

    【讨论】:

    • 如示例所说,我累了,我不想尝试会话属性,坦率地说我从未使用过它们,所以我对它们了解不多。
    • 通过为“editForm”添加会话属性注释,您基本上可以使这个模型属性在会话中生效,我相信这应该可以解决问题
    猜你喜欢
    • 2012-08-12
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多