Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How do i bind nested model objects to form fields?

  1. #1
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default How do i bind nested model objects to form fields?

    Hi,

    I have a problem with Spring's form binding:

    Say I have a "CAR" model class passed to a jsp form and this class has a property which is a "Driver" class. How are we supposed to tell Spring to bind jsp form field this sub-object??? I get errors whenever i try to submit the form.

    Does it have something to do with propertyEditors? Or are we constrained in having all attributes of the model be simple data types?

    Any help would be appreciated!

    Uze

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    Using initBinder() in the form controller (or whatever other command controller) you can register your own custom property editors. Using those you can resolve other than primitive types.

    Implement PropertyEditorSupport and override setAsText and getAsText:

    Code:
    DriverEditor extends PropertyEditorSupport {
    
      public void setAsText(String text) {
        // text is the string from the form, for example an identifier in
        // the database
        
        // use whatever you like to retrieve the appropriate Driver object
        Driver d; // retrieve
        setValue(d);
      }
    
      public String getAsText(Object value) {
        Driver d = (Driver)value);
        return d.getId(); // for example
      }
    }
    Let's say your command name is car.

    Code:
    <select name="car.driver">
      <option value="1">Driver one</option>
      <option value="2">Driver two</option>
    </select>
    1 resp. 2 would be the String passed to your custom editor.

    Finally, register your editor using

    Code:
    binder.registerCustomEditor&#40;Driver.class, "driver" /* this is optional */, new DriverEditor&#40;&#41;&#41;;
    Hope this helps.

    Alef

  3. #3
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    You probably need to initialize the nested object in its parent. For example:

    Code:
    public class Car &#123;
    
        private Driver driver = new Driver&#40;&#41;;
    
    &#125;

  4. #4
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    Quote Originally Posted by mraible
    You probably need to initialize the nested object in its parent. For example:

    Code:
    public class Car &#123;
    
        private Driver driver = new Driver&#40;&#41;;
    
    &#125;
    Yes of course, if you need to bind a primitive value to the driver property of the car object.

    Code:
    <input type="text" name="car.driver.name">
    You wouldn't need to bind customer editors in this cas.e

    Alef

  5. #5
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    Thanks a lot Alef, this is what I was looking for. However, where do you call the binder.registerCustomEditor? Is it to be called in a specific method of a controller servlet (ie initBinder) or declared in the xyz-servlet.xml config file under a form bean definition? A short example would help a lot.

    Thanks again!

    Uze

  6. #6
    Join Date
    Aug 2004
    Location
    Mount Joy, PA
    Posts
    34

    Default

    However, where do you call the binder.registerCustomEditor? Is it to be called in a specific method of a controller servlet (ie initBinder)
    initBinder is the method you're looking for. If you read the JavaDoc for BaseCommandController, esp. the initBinder method, you should find all you need to know. The references to registerCustomEditor are also helpful.
    ~ Daniel Miller

  7. #7
    Join Date
    Aug 2004
    Location
    Montreal, Canada
    Posts
    35

    Default

    So far so good, however I was more looking at a global definition, avoiding the need to register the same editors over and over again in many controllers. From the Spring doc, it appears to be doable in the application context with something like:

    Code:
    	 <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    	   <property name="customEditors">
    	     <map>
       
    	       <entry key="mypackage.Driver">
    	         <bean class="mypackage.UserEditor" />
    	       </entry>
    	       
    	     </map>
    	   </property>
    	 </bean>
    but it does not seem to pick it up and my custom editor is never called.

    Any ideas????

  8. #8
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    The CustomEditorConfigurers only apply to property values used in the application context, so they don't get registered in your controllers. You have to register them yourself, either by doing that programmatically in every controller or by subclassing the command controller you're using and creating a simple structure yourself in which you can mention property editors.

    Remember that property editors and not threadsafe, so if you decide to go and implement somsething yourself that registers default property editors in all your controller, keep in mind that you need to create them over and over again instead of re-using one instance.

    Alef

  9. #9
    Join Date
    Aug 2004
    Location
    San Jose, CA
    Posts
    24

    Default Data access in property editor

    This thread has been informative. However, I'm still a little confused about property editors. I've got a business object, Job, that is selected from the UI by ID. However, my property editor needs access to the database to resolve the ID to a Job object. I've configured the following bean in *-servlet.xml where the appConfig bean has all of the data access stuff in it.
    Code:
    	<bean id="jobPropertyEditor" class="com.myapp.util.propertyEditors.JobEditor">
    	    <property name="appConfig">
    			<ref bean="appConfig"/>
    		</property>
    	</bean>
    In my controller I register my editor as such
    Code:
    protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41;
            throws Exception &#123;
        binder.registerCustomEditor&#40;Job.class, "job", &#40;JobEditor&#41; getApplicationContext&#40;&#41;.getBean&#40;
                "jobPropertyEditor"&#41;&#41;;
    &#125;
    Is this the correct way to wire things up? It seems to work. Any guidance would be much appreciated.

    - Justin

  10. #10
    Join Date
    Aug 2004
    Posts
    3

    Default

    Quote Originally Posted by Alef Arendsen
    Quote Originally Posted by mraible
    You probably need to initialize the nested object in its parent. For example:

    Code:
    public class Car &#123;
        private Driver driver = new Driver&#40;&#41;;
    &#125;
    Yes of course, if you need to bind a primitive value to the driver property of the car object.
    Code:
    <input type="text" name="car.driver.name">
    You wouldn't need to bind customer editors in this cas.e
    Alef
    ===================================
    Alef,

    I'm kind of new to spring and am struggling with this same issue. Can you explain why you don't need a property editor in this case when you bind a primitive value to the driver property in the car object? What differentiates these 2 cases?

    Also, why does Matt's solution work? I implemented it and it does wortk, but I want to understand what's going on here. It seems kind of strange that one needs to create a new, basically empty enclosed class [in this case, the Driver object] when there wopn't be any real values in that class anyway, since it's just getting created without any values when the class is loaded.

    Thanks much!

    -=j=-

    [jack]

Similar Threads

  1. Replies: 6
    Last Post: Sep 24th, 2006, 11:58 AM
  2. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  3. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  4. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  5. Replies: 8
    Last Post: Nov 17th, 2004, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •