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

Thread: [MVC] Home made object binding

  1. #1

    Default [MVC] Home made object binding

    Hy everybody,

    I'm new in the famous Srping world. I'm trying to write a form with à <form:select /> in this drop down list i put a List of Section objects.
    Code:
    <form:select path="section">
      <form:option value="null" label="" />
      <form:options items="${sections}" itemLabel="name"/>                        
    </form:select>
    But when i try to submit this form i get this error :
    Failed to convert property value of type [java.lang.String] to required type [be.me.commons.beans.Section] for property section; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [be.me.commons.beans.Section] for property section: no matching editors or conversion strategy found
    Must i create one PropertyEditor for each Object that i need to use in my form ?

    Edit : It seems yes, i must create a PropertyEditor for each objects -> http://www.jroller.com/raible/entry/...ith_spring_mvc


    Thanks
    Last edited by blaiseg; Dec 27th, 2007 at 04:52 AM.

  2. #2

    Unhappy

    Nobody to help me ?

  3. #3
    Join Date
    Oct 2005
    Location
    Amsterdam
    Posts
    123

    Default

    What does your Section class look like?
    Hans Westerbeek
    Software Engineer

  4. #4

    Default

    She looks like that :
    Code:
    public class Section {
        
        private int id;
        private Level level;
        private String name;
        private String description;
        
        
        public Section() {
            this(0, null, null, null);
        }
    
        public Section(String name, Level level) {
            this(0, name, level, null);
        }
        
        public Section(int id, String name, Level level, String description) {
            this.id = id;
            this.level = level;
            this.name = name;
            this.description = description;
        }
    
        
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Level getLevel() {
            return level;
        }
        
    }
    And maybe the Level class :
    Code:
    public class Level {
    
        private int id;
        private String name;
        private String description;
        
        public Level() {
            this(0, null, null);
        }
        
        public Level(String name) {
            this(0, null, null);
        }    
        
        public Level(int id, String name, String description) {
            setId(id);
            setName(name);
            setDescription(description);
        }
    // Getters and Setters 
    }
    Thank you

  5. #5
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    Could you post your <form:form> tag? What class is "bound" there?

  6. #6
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    Not sure if this will help, but here is a short example:

    jsp:
    http://jmemento.svn.sourceforge.net/...sp?view=markup

    controller:
    http://jmemento.svn.sourceforge.net/...va?view=markup

    model/domain/whatever:
    http://jmemento.svn.sourceforge.net/...va?view=markup
    http://jmemento.svn.sourceforge.net/...va?view=markup

    Notice that there's an "outer" bean, Command2, that's bound to the overal form. And then there's an "inner" bean, an ArrayList of FormOption, that is used for the select drop down list. Selecting an option sets a property/field (selectPath) on the Command2 bean. That's my understanding of how to do it.

  7. #7

    Default

    Yes, sure. Here is my <form> :
    HTML Code:
    <form:form commandName="studentfolder">
    and my controller :
    Code:
    public class RegistrationController extends SimpleFormController {
    ...
      protected Map referenceData(HttpServletRequest request) throws Exception {
            Map map = new HashMap(1);        
    
            map.put("sections", sectionManager.getSections()); // Section manager return a java.util.LIst<Section>
            return map;
        }
    ...
    }

    Quote Originally Posted by lumpynose View Post
    Not sure if this will help, but here is a short example: ...
    Notice that there's an "outer" bean, Command2, that's bound to the overal form. And then there's an "inner" bean, an ArrayList of FormOption, that is used for the select drop down list. Selecting an option sets a property/field (selectPath) on the Command2 bean. That's my understanding of how to do it.
    Yes i understand it like that and i think that i've doing that. But when i submit my form Spring can't bound the string (aka section.id) to my object (section) ..
    Last edited by blaiseg; Jan 2nd, 2008 at 02:49 AM.

  8. #8
    Join Date
    Sep 2007
    Posts
    19

    Default

    You can create a custom org.springframework.beans.propertyeditors.ClassEdi tor for your Section class to translate a identifying string value from the HTML page into a Section instance. This is especially useful if you persist your Section objects with some sort of unique identifier (like a database primary key).

    We usually only have to resort to making our own Property Editors when we want to represent something as a select list or checkboxes/radio buttons. It's not that bad, and if you're only doing it in one controller, you can just define it inline.

    In your controller:
    Code:
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
    {
      binder.registerCustomEditor(Section.class, "section", new ClassEditor() {
          public String getAsText() {
              Object value = getValue();
              return value == null ? "" : new Integer(((Section)value).getId()).toString();
          }
          public void setAsText(String text) throws IllegalArgumentException {
              try {
                  int id = new Integer(text);
                  Section section = someServiceObj.getSectionById(id);
                  if (section == null) {
                      throw new IllegalArgumentException("Invalid id for Section: " + text);
                  }
                  setValue(section);
              } catch (NumberFormatException e) {
                  throw new IllegalArgumentException("Invalid format for Section id: " + text);
              }
          }
      });
    }
    Then, in your JSP, just use the Section id as the value for your option tags and its name or description as the label.

    Hope that helps,
    Mike

  9. #9

    Thumbs up

    Hy, thanks for you help.

    Yes that help me.
    So if i have 5 differents beans in my form i must write 5 propertyEditor. Does not Spring provide a Generic propertyEditor ?

  10. #10
    Join Date
    Sep 2007
    Posts
    19

    Default

    You only have to write property editors for objects that Spring doesn't know how to bind from the HTML view for you.

    API Documentation

    You'll probably find yourself defining custom date editors for user-friendly date format entry, and as you've already seen, you'll be defining custom class editors when you need to use a select/option list. You'll use byte array editors if you're doing file uploads, and you'll use a custom collection editor when you need to bind to a collection of objects (like when you use checkboxes).

Posting Permissions

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