0

I'm trying to use @modelAttribute to send my model attributes to the controller

my model contains many attributes (String, Integer,..) one of them is an object that i want to retrieve from a select tag. the problem is when i pass the modelattribute to the controller my object is Null

JSP:

<form:form method="post" action="saveUorg.html"  modelAttribute="uorg" >
<table >
<tr>
    <th>Nom</th>
    <th>Nom abregé</th>
    <th>timbre</th>
    <th>Date début effet</th>
    <th>Date fin effet</th>
</tr>
<tr>
    <td><input  path="nom" name="nom"/></td>
    <td><input  path="nomAbrege" name="nomAbrege"/></td>
    <td><input  path="timbre" name="timbre"/></td>
    <td><input  type="date" path="dateDebutEffet" name="dateDebutEffet"/></td>
    <td><input  type="date" path="dateFinEffet" name="dateFinEffet"/></td>
 </tr>
</table> 
<table >
<tr>
    <th>email</th>
    <th>Unité père</th>
</tr>
<tr>
    <td><input  path="email" name="email"/></td>
    <td><select  path="refUniteOrganisParent">
        <option  value="-"> --- </option> 
      <c:forEach items="${listeuos}" var="uorgg" varStatus="status" >
        <option  value="${uorgg}">${uorgg} </option> 
      </c:forEach>
    </select></td>
 </tr>

this is my controller

    @RequestMapping(value ="/saveUorg", method = RequestMethod.POST)
    public ModelAndView saveUorg(@ModelAttribute("uorg") UorgVO uorg,BindingResult result){


        System.out.println("RefUniteOrganisParent:" +uorg.getRefUniteOrganisParent());


        return new ModelAndView("view","uorg",uorg);    
    }   

refUniteOragnisParent is the null object, when i print the content result in my controller of uorg.refUniteOrganisParent the result is null. Thank's in advance for help.

2
  • Signature of the controller method? Class? Commented Jun 28, 2013 at 15:42
  • @zeroflagL i edited the post and put my controller method Commented Jun 28, 2013 at 15:49

2 Answers 2

2

First your select tag does not have a name attribute.

Second when a form is submitted the controller only gets strings. Spring has to convert every parameter to the type you want. It has built-in converters for simple types like Integer or Boolean, but not for complex types, not to mention your own types.

So if the attribute refUniteOrganisParent is an object and only represented by a single value (the option value) you need to implement a converter that creates an instance based on this value:

public class StringToMyType implements Converter<String, MyType> { ...

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/validation.html#core-convert

And you need to register your Converter: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-customize

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

Comments

0

In your controller,the model attribute object should be like this:

        @RequestMapping(value ="/saveUorg", method = RequestMethod.POST)
       public ModelAndView saveUorg(@ModelAttribute("uorg") UorgVO uorg,BindingResult result){

          ModelAndView mav = new ModelAndView("view");              
          mav.addObject("uorg",uorg);
          System.out.println("RefUniteOrganisParent:" +uorg.getRefUniteOrganisParent());

    return mav;    
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.