我使用spring mvc 3.1.0和jsp/jstl。 要將對象提交給我的控制器,我使用了ModelAttribute註釋,並且所有工作都正常。 但是,當我嘗試將一個複雜對象提交給我的控制器時,它的值爲空。 這是我的對象模型:使用ModelAttrbute註釋從jsp提交對象到控制器
UorgVO.java
public class UorgVO {
private String nom;
private String nomAbrege;
private UorgVO refUniteOrganisParent;
//getters&Setters..
}
,並有我的jsp頁面:
<form:form method="post" action="saveUorg.html" modelAttribute="uorg" >
<table>
<tr>
<th>Nom</th>
<th>Nom abregé</th>
<th>Unité père</th>
</tr>
<tr>
<td><input type="text" path="nom" name="nom"/></td>
<td><input type="text" path="nomAbrege" name="nomAbrege"/></td>
<td>
<select id="refUniteOrganisParent"
name="refUniteOrganisParent"
path="refUniteOrganisParent">
<option value="null"> --- </option>
<c:forEach items="${listeuos}" var="uorgg" varStatus="status" >
<option value="${uorgg}">${uorgg} </option>
</c:forEach>
</select>
</td>
</tr>
</table>
<input type="submit" value="Enregistrer uorg"/>
</form:form>
我的控制器:
@RequestMapping(value ="/saveUorg", method = RequestMethod.POST)
public ModelAndView saveUorg(@ModelAttribute("uorg") UorgVO uorg,BindingResult result) {
System.out.println("My foreign attribute is:" +uorg.getRefUniteOrganisParent());
return new ModelAndView("uorg_recherche");
}
和印刷值爲null,但我的對象的其他屬性被提交。
感謝的提前幫助
不確定的答案,但看起來有一個在你的JSP語法錯誤 - 行'你不關閉輸入標籤,並且''標籤看起來應該不存在。 –
robjohncox
我刪除了標籤,但仍然無法使用。 – Mouhie
使用Firebug或類似軟件,你能否確認你的瀏覽器在提交時肯定會發送'refUniteOrganisParent = value'參數?此外,您將無法將此值直接綁定到「UorgVO」 - 因爲傳輸的值將是一個字符串。您需要在控制器中註冊一個「PropertyEditor」,該控制器將傳輸的字符串轉換爲「UorgVO」。看到這個[示例](https://raymondhlee.wordpress.com/2011/06/08/using-initbinder-in-spring-3-controller/) –