Pergunta

I'm using spring mvc 3.1.0 with jsp/jstl. To submit object to my controller I'm using the ModelAttribute annotation and all is working fine. But, when I try to submit a complex object to my controller his value is null. This is my object model:

UorgVO.java

public class UorgVO {

    private String nom;
    private String nomAbrege;
    private UorgVO refUniteOrganisParent;

    //getters&Setters..
}

and there is my jsp page:

<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>

And my controller is:

@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");  
}   

And the printed value is null, but the other attributes of my object are submitted.

Thank's in advance for help

Foi útil?

Solução

I solved the problem, i hope that it gonna help other users who has the same problem. By using the service of ConversionServiceFactoryBean. First, i used the ' form:select ' and 'form:input' , there is my new jsp code :

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

As you can see i retried the id value of the selected item.

Secondly i used this converter:

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;        
  }

}

After that i configure my xml file by doing this:

<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>

Thank's to everybody who participates in this post to solve the problem.

Mouhie.

Outras dicas

If you go to the site with form, you should send model. The best way is return new ModelAndView("site","uorg", new UorgVO()) from your controler where method = GET.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top