Results 1 to 2 of 2

Thread: Access map elements through a new BeanWrapperImp(HashMap)

  1. #1

    Default Access map elements through a new BeanWrapperImp(HashMap)

    I would like to use a BeanWrapper (or similar) to access properties of objects contained in a map, somewhat like this:


    // Wiring
    Person p = new Person (...);
    p.setFullName("Eirik Lygre")
    Map bean = new HashMap();
    bean.put ("user", p);
    BeanWrapper wrapper = new BeanWrapper (bean)

    // Set property on person
    wrapper.set ("user.fullName", "Eirik Lygre")


    This does not currently work, and there is an open JIRA-request for it (https://jira.springsource.org/browse/SPR-2058; BeanWrapper does not support objects that are maps/arrays/lists themselves). My question is therefore as follows:


    • Is there a well-known workaround, ie through configuring or subclassing the BeanWrapper, or creating some sort of magic around it?
    • Is there any other Spring-related technology that will do this for me?
    • Is there any other non-spring related technology that will do this for me?


    Eirik

  2. #2
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    133

    Default

    For example, you can use a MapHolder and access to property map['user'']

    Code:
    BeanWrapper wrapper = new BeanWrapperImp(new MapHolder(map));
    
    public class MapHolder {
    	private Map map;
    
    	public MapHolder(Map map) {
    		this.map = map;
    	}
    
    	public Map getMap() {
    		return map;
    	}
    
    	public void setMap(Map map) {
    		this.map = map;
    	}
    }
    Cheers
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

Posting Permissions

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