0

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

4
  • Not sure of the answer, but there looks to be a syntactic error in your JSP - on the line <input type="submit" value="Enregistrer uorg" <BQ> you don't close the input tag, and the <BQ> tag looks like it shouldn't be there. Commented Jul 3, 2013 at 15:01
  • i removed the tag and still not work. Commented Jul 3, 2013 at 15:32
  • Using Firebug or similar, can you confirm that your browser is definitely sending the refUniteOrganisParent=value parameter when you submit? Also, you won't be able to bind this value directly to a UorgVO - because the transmitted value will be a string. You'll need to register a PropertyEditor in the controller which will convert the transmitted string to a UorgVO. See this example Commented Jul 5, 2013 at 10:47
  • Yes, i used a converter to solve the problem with ConversionServiceFactoryBean Service i'm writing the answer on the post. Thank you Will Keeling. Commented Jul 5, 2013 at 10:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

That does not answer the question
@user902691 which site and controller are you talking about ? the page who refers to my form page??
@mouhie I talk about entry site, where you return View name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.