2013-07-03 75 views
0

我使用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,但我的對象的其他屬性被提交。

感謝的提前幫助

+0

不確定的答案,但看起來有一個在你的JSP語法錯誤 - 行'你不關閉輸入標籤,並且''標籤看起來應該不存在。 – robjohncox

+0

我刪除了標籤,但仍然無法使用。 – Mouhie

+0

使用Firebug或類似軟件,你能否確認你的瀏覽器在提交時肯定會發送'refUniteOrganisParent = value'參數?此外,您將無法將此值直接綁定到「UorgVO」 - 因爲傳輸的值將是一個字符串。您需要在控制器中註冊一個「PropertyEditor」,該控制器將傳輸的字符串轉換爲「UorgVO」。看到這個[示例](https://raymondhlee.wordpress.com/2011/06/08/using-initbinder-in-spring-3-controller/) –

回答

1

我解決了這個問題,我希望它會幫助其他用戶誰具有相同的問題。 通過使用ConversionServiceFactoryBean的服務。 首先,我用了「形式:選擇」和「形式:輸入」,還有就是我的新JSP代碼:

<td><form:input path="email" type="text"/></td> 
<td> 
<form:select path="refUniteOrganisParent.id" items="${listeuos}" itemValue="id" itemLabel="nom"/> 
</td> 

正如你可以看到我試值所選項目的ID。

其次我用這個轉換器:

public class UorgConverter implements Converter<Long, UorgVO>{ 

@Autowired 
private UorgService uo; 

@Override 
public UorgVO convert(Long id) { 

    // actions dealing with the service, to retrieve Uorg object from the id sended by the jsp page 

    return uorgItem;   
    } 

} 

之後配置我的xml文件通過這樣做:

<mvc:annotation-driven conversion-service="conversionService"/> 

<bean id="conversionService" class="my.package.ConversionServiceFactoryBean"> 
<property name="converters"> 
    <list> 
     <bean class="converter.package.location.UorgConverter"></bean> 
     <!--List of my converters --> 
    </list> 
</property> 
</bean> 

感謝對大家誰在這個崗位參與解決問題。

Mouhie。

0

如果你去與形式的網站,你應該送模型。最好的方法是從您的控制器 return new ModelAndView("site","uorg", new UorgVO())其中method = GET

+0

這並不回答 – zeroflagL

+0

@ user902691問題,你在說哪個站點和控制器?引用我的表單頁面的頁面? – Mouhie

+0

@mouhie我談論入口網站,你返回'View'名稱。 – user902691

相關問題