在下面的Spring窗體中,我的對象ID字段已填充,但是當我在控制器方法中接收到提交時,除ID字段外,所有窗體的字段都已填充。我已經五元組檢查過,字段類型和getter/setter類型都是非原始類型,因爲我已經看到許多類似SO的其他問題,這似乎是常見問題。該控制器沒有任何方法級@ModelAttributes,因此它不會被填充。未在POST上填充彈簧窗體ID字段
這裏的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>
我用鉻devtools趕上POST數據,在這裏,它是:
id=1&name=Organization1&description=
...但在在editOrganizationPost方法的第一行設置斷點,該form.id場設置爲0.
我花了很長時間試圖弄清楚爲什麼它會綁定名稱和描述,但不是發送時的ID。我顯然可以從路徑變量中注入ID,但我爲什麼不自然地填充該字段而目瞪口呆。
您不需要使用Spring的非原始包裝工作。這只是使得值*可能爲空。無論如何,您是否可以使用瀏覽器調試工具(Chrome的開發工具,FireFox的FireBug,IEs開發工具)來檢查您的瀏覽器發佈的內容?我只是想確保它實際上發佈了id字段。另外,在渲染之前是否可以看到.jsp表單?我特別想看看你如何編碼輸入(JSTL彈簧標籤等)。 – CodeChimp
我編輯了帖子以包含JSP表單和POST數據。 –
不確定它是否可以解決問題,但是我對框架的使用教會我總是在課後給方法參數命名,例如'OrganizationForm organizationForm'而不是'OrganizationForm orgForm'。 – Bart