Announcement Announcement Module
Collapse
No announcement yet.
Initializing an object tree for Spring MVC binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initializing an object tree for Spring MVC binding

    If I have a form-backing object that has a complicated object tree -- say a Person that has a Contact Info object that has an Address object that has a bunch of Strings -- it seems that the object needs to be fully populated with component objects before I can bind to it. So if I'm creating a new Person, I need to make sure it has all the component objects populated off the bat, and if I'm retrieving a Person from the database, I need to make sure that any objects that aren't populated from the database get populated with empty objects.

    First question, of course -- am I correct in my assumptions above? It does seem that if I try to bind to person.contactInfo.homeAddress.street and there is no ContactInfo, I get a null pointer exception.

    Second, what's the best way to initialize my object? Is there any way to initialize an object on the fly at the time of binding?

    Any suggestions are appreciated!

  • #2
    Originally posted by jacobmattison View Post
    If I have a form-backing object that has a complicated object tree -- say a Person that has a Contact Info object that has an Address object that has a bunch of Strings -- it seems that the object needs to be fully populated with component objects before I can bind to it. So if I'm creating a new Person, I need to make sure it has all the component objects populated off the bat, and if I'm retrieving a Person from the database, I need to make sure that any objects that aren't populated from the database get populated with empty objects.

    First question, of course -- am I correct in my assumptions above? It does seem that if I try to bind to person.contactInfo.homeAddress.street and there is no ContactInfo, I get a null pointer exception.

    Second, what's the best way to initialize my object? Is there any way to initialize an object on the fly at the time of binding?

    Any suggestions are appreciated!
    Hello Jacob.
    I think its wrong way - create object and initialize him from DB and put that in to a web layer. All object that you send to web layer must be a data units objects (may be its my mistake ?!). Try next
    DataBase --> PortalDao --> PortalService -->Web Model

    and sample data unit

    AbstractDataUnit
    Code:
    public class AbstractDataUnit {
    
    	public static AbstractDataUnit newInstance(Object candidate)
    			throws ServiceException {
    		throw new NotImplementedException("Non implemented method ");
    
    	}
    
    }
    and

    UserDataUnit
    Code:
    public class UsersDataUnit extends AbstractDataUnit implements Serializable {
    
    	private static final long serialVersionUID = -1L;
            ......
            //person.contactInfo.homeAddress.street
            private String street;
            .........
            
            public UsersDataUnit() {
    	}
            public UsersDataUnit(String street) {
                this.street = street;
    	}
            
            //now getter and setter for property
    
            ........
           // and newInstance
    
            public static AbstractDataUnit newInstance(Object candidate)
    			throws ServiceException {
    		if (candidate == null) {
    			return new UsersDataUnit();
    		}
    
    		UsersDataUnit usersDataUnit = null;
    
    		try {
    			Users obj = (Users) candidate; //POJO
    			usersDataUnit = new UsersDataUnit(obj.getContactInfo.getHomeAddress.getStreet()} catch (DataAccessException e) {
    			throw new UnableLoadDataException(e);
    		}
    
    		return usersDataUnit;
    	}
    }
    and sure that:

    PortalService
    Code:
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    	public UsersDataUnit getUser()
    			throws ServiceException {
    		Users user = null;
    		try {
                            //get a user object
    			user = this.getPortalDao().getUser();
    		} catch (DataAccessException e) {
    			throw new UnableLoadDataException(e);
    		}
    
    		return (UsersDataUnit) UsersDataUnit.newInstance(user);
    	}
    Thats all.

    Comment


    • #3
      Thanks for replying.

      I'm not sure I understand your suggestion. Somebody somewhere needs to say

      myPerson.ContactInfo = new ContactInfo()

      before the form can bind to fields within ContactInfo, right? So where should this happen? That's my question.

      Yes, certainly, I'm getting my objects from the DAO when appropriate (i.e. when it's a returning person rather than a brand new person).

      Are you suggesting that the form-backing object should not be a POJO but something else -- that my Person or Candidate or whatever shouldn't be the form-backing object?

      I don't know what you mean by a "data units object". Also not sure what your references to "portal" refer to -- I'm discussing Spring MVC, typically using a SimpleFormController.

      Comment


      • #4
        http://img297.imageshack.us/img297/6...ponentsry9.png


        Data Unit - business logic bean. Created from a Data access object. Only a business logic bean You will can put in Web logic layer - no pojo object.

        In my code (my first post in this thread) i try to explain how to create business logic bean.

        Comment

        Working...
        X