StrutsCatalog: Provide a mapped bean that can be nested in in <bean:write>. April 24, 2004

This BeanMap class provides you with a bean that allows you to access map values using <bean:write>. Assume that you have the following code somewhere:

BeanMap beanMap1 = new BeanMap();
BeanMap beanMap2 = new BeanMap();
beanMap2.setProperty("A","Hello, World!");
beanMap1.setProperty("B",beanMap2);

And, assume that you have set your beanMap1 in some scope on your website, e.g.

session.setAttribute("C",beanMap1);  

Now, you can access the entry in beanMap1 as follows:

<bean:write name="C" property="this(B).map(A)"/>

This will output:

Hello, World!

Also, cf. StrutsCatalogInstrumentableForms,StrutsCatalogVariableScreenFields, and http://jakarta.apache.org/struts/faqs/indexedprops.html

   
public class BeanMap 
    implements Map {
  private Map map;

  public BeanMap() {
    int size = 89;
    this.map = Collections.synchronizedMap(new HashMap(size));
  }

  public void setMap(Map map) {
    this.map = map;
  }

  public Map getMap() {
    return map;
  }

  public Map getThis() {
    return this;
  }

  public void setProperty(Object key, Object value) {
    map.put(key,value);
  }

  public Object getProperty(Object key) {
    return map.get(key);
  }

  public void       clear()                       { map.clear(); }
  public boolean    containsKey(Object key)       { return map.containsKey(key);}
  public boolean    containsValue(Object value)   { return map.containsValue(value); }
  public Set        entrySet()                    { return map.entrySet(); }
  public boolean    equals(Object object)         { return map.equals(object); }
  public Object     get(Object key)               { return map.get(key); }
  public int        hashCode()                    { return map.hashCode(); }
  public boolean    isEmpty()                     { return map.isEmpty(); }
  public Set        keySet()                      { return map.keySet(); }
  public Object     put(Object key, Object value) { return map.put(key,value); }
  public void       putAll(Map map)               { map.putAll(map); }
  public Object     remove(Object key)            { return map.remove(key); }
  public int        size()                        { return map.size(); }
  public Collection values()                      { return map.values(); }
  public String     toString()                    { return map.toString(); }
} ///;-)

– Michael McGrady

  • No labels