Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: [MVC] Home made object binding

  1. #11
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Quote 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!
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  2. #12
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    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.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  3. #13

    Default

    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.

  4. #14
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    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').
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  5. #15

    Default

    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.

  6. #16
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Quote Originally Posted by blaiseg View Post
    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.
    OK, so yes, I agree with you

    And it sounds like you're on the right track. Writing a custom PropertyEditor isn't hard, but it's not something I can really help you do

    Look at some of Spring's Custom*Editor classes for guidance. Good luck!
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  7. #17

    Smile

    Thanks.

    I think at something like that for my generic editor :
    Code:
    public interface IdBeanManager {
       // I don't like that because i get an object but not the real type.
      public Object getObject(int id);
    }
     
    public class MonBeanManager implements IdBeanManager {
       private MonBeanDao dao;
       ...
       public Object getObject(int id) {
         return dao.getBean(id);
       }
    }
     
    public class GenericPropertyEditor extends PropertyEditor {
      private IdBeanManager manager;
      ...
      public Object getAstText(String text) {
        try {
          return manager.getObject(Integer.parseInt(text));
        } catch (...) { .. }
      }
    }

Posting Permissions

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