POST上未填充Spring表单ID字段

时间:2014-06-24 23:40:56

标签: java forms spring spring-mvc

在下面的Spring表单中,填充了我的对象的ID字段,但是当我在控制器方法中收到提交时,除了ID字段之外,将填充所有表单的字段。我已经五次检查了字段类型和getter / setter类型都是非原始的,因为我已经看到很多关于SO的其他问题与此类似,这似乎是常见的问题。控制器没有任何方法级别的@ModelAttributes,因此不会被填充。

这里是POST方法的声明,因为我在第一个包含行上调试它,发现表单的id字段为空:

    @RequestMapping(value="/{orgId}", method=RequestMethod.POST)
public String editOrganizationPost(@PathVariable int orgId,
        @Valid @ModelAttribute(ORG_FORM) OrganizationForm orgForm,
        BindingResult result, RedirectAttributes att,
        HttpServletRequest request) {

这是表单对象:

public class OrganizationForm {

private Integer id;
@NotBlank
private String name;
@NotBlank
private String description;
private Set<User> users;

int moveToOrganizationId = 0;
String moveToOrganizationName;
int[] moveFromOrganizationUserSelect = null; // List of selected users
                                                // to be moved to a new
                                                // organization

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Set<User> getUsers() {
    return users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

public int getMoveToOrganizationId() {
    return this.moveToOrganizationId;
}

public void setMoveToOrganizationId(int moveToOrganizationId) {
    this.moveToOrganizationId = moveToOrganizationId;
}

public String getMoveToOrganizationName() {
    return this.moveToOrganizationName;
}

public void setMoveToOrganizationName(String moveToOrganizationName) {
    this.moveToOrganizationName = moveToOrganizationName;
}

public int[] getMoveFromOrganizationUserSelect() {
    return this.moveFromOrganizationUserSelect;
}

public void setMoveFromOrganizationUserSelect(
        int[] moveFromOrganizationUserSelect) {
    this.moveFromOrganizationUserSelect = moveFromOrganizationUserSelect;
}

public boolean isNew() {
    return this.id == null || this.id == 0;
}

}

以下是JSP文件的标记:

<form:form method="post" action="${submitUrl}" commandName="organizationForm">
                <form:errors path="*" />
                <form:hidden path="id" />
                <table class="adminTable editContent">
                    <tr class="bg_lgtGrey">
                        <td><fmt:message
                                key="manageOrganizations.organizationForm.name" />:</td>
                        <td><form:input path="name" cssClass="inputbox"
                                tabindex="4" /></td>
                    </tr>
                    <tr class="bg_lgtGrey">
                        <td><fmt:message
                                key="manageOrganizations.organizationForm.description" />:</td>
                        <td><form:textarea path="description"
                                cssClass="inputbox" tabindex="4" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><c:choose>
                                <c:when test="${!organizationForm.new}">
                                    <input type="submit" class="btn btn-primary"
                                        id="submit_button" value="Update" />
                                </c:when>
                                <c:otherwise>
                                    <input type="submit" class="btn btn-primary"
                                        id="submit_button" value="Create" />
                                </c:otherwise>
                            </c:choose></td>
                    </tr>
                </table>
            </form:form>

这是生成的HTML:

<form id="organizationForm" action="/admin/organizations/1" method="post">

                <input id="id" name="id" type="hidden" value="1">
                <table class="adminTable editContent">
                    <tbody><tr class="bg_lgtGrey">
                        <td>Organization Name:</td>
                        <td><input id="name" name="name" class="inputbox" tabindex="4" type="text" value="Organization1"></td>
                    </tr>
                    <tr class="bg_lgtGrey">
                        <td>Organization Description:</td>
                        <td><textarea id="description" name="description" class="inputbox" tabindex="4"></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right">

                                    <input type="submit" class="btn btn-primary" id="submit_button" value="Update">


                            </td>
                    </tr>
                </tbody></table>
            </form>

我使用Chrome devtools来捕获POST数据,这里是:

id=1&name=Organization1&description=

...但是在editOrganizationPost方法第一行的断点处,form.id字段设置为0.

我花了很长时间试图弄清楚为什么它会在发送时绑定名称和描述而不是ID。我显然可以从路径变量中注入ID,但我很傻到为什么它不会自然地填充该字段。

1 个答案:

答案 0 :(得分:0)

我有同样的问题.. id字段总是设置为0.将id字段更改为其他字段也不起作用。经过更多的追踪和追踪,删除

disabled="true"

<form:input path"id" disabled="true"/>

解决了这个问题。 我以为 <form:hidden path="id"> 会给出同样的错误,但事实并非如此。