Announcement Announcement Module
Collapse
No announcement yet.
[MVC] Home made object binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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, 04:52 AM.

  • #2
    Nobody to help me ?

    Comment


    • #3
      What does your Section class look like?

      Comment


      • #4
        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

        Comment


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

          Comment


          • #6
            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.

            Comment


            • #7
              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;
                  }
              ...
              }

              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, 02:49 AM.

              Comment


              • #8
                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

                Comment


                • #9
                  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 ?

                  Comment


                  • #10
                    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).

                    Comment


                    • #11
                      Originally posted by blaiseg View Post
                      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 ?
                      The most common scenario where you need a custom property editor is if you are trying to resolve a form element single value to an object. For example, in your case you are submitting the section 'id' as the form field value, and are expecting this to resolve into an object of type Section (which may involve database access, etc). Spring isn't going to handle this for you, because very often the solution will completely depend on the system design/business model. What you will probably find is that within your application, you may have some common functionality that can be generalized into a single PropertyEditor which can be used on many types of objects (for example, in data-driven applications, a property editor that does "resolve object of type X by id" is quite common).

                      Also, I don't believe 'mbeards' is quite right - you should be extending the class java.beans.PropertyEditorSupport for your custom PropertyEditors - not the Spring 'ClassEditor', which has a specific purpose (resolving class names to java.lang.Class) and isn't intended to be used as a base class for other property editors.

                      Hope that helps!

                      Comment


                      • #12
                        Sorry, I realized I didn't completely answer your question. Spring does provide PropertyEditors for conversion between String (which is the type of all submitted request parameters) and many common property editors which will convert primitives and other basic data types. Have a look at this section of the Spring docs for a list of these.

                        Comment


                        • #13
                          Ok, si for each personnal bean i need to write a PropertyEditor. But Spring give me some support class for PropertyEditor and maybe i can write a Generic PropertyEditor.

                          Because i have many personnel beans i like the last ideaos (Generic property editor). Now i need to know how to write this editor...

                          If you want help me, i'm in a data-driven application with some managers that works as Facade (design pattern) and provide CRUD methods.

                          Comment


                          • #14
                            PropertyEditors are not bound to beans, they are bound by type (Class). So you need to write a custom PropertyEditor for each type binding in your application, not each bean (I assume, by 'bean', you mean 'command object').

                            Comment


                            • #15
                              Yes, they are bound to Class. But when i say 'bean' i think 'class'.

                              PropertyEditors are used to bind a object of a given class from a form value.

                              Comment

                              Working...
                              X