0

Suppose my model field is a HashMap of Strings to ArrayList's of Strings.

HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();

For field binding, I need to point to an i-th item of the ArrayList of a specific key in my JSP. How do I do it? Will this double-bracket notation work? Assume that 'key' and 'index' are known JSTL variables in my JSP.

<form:checkbox path="map[${key}][${index}]" />

What I'm essentially doing is, for a field value, store its path e.g.

map['testkey'][4]

which would point to

map.get("testkey").get(4);

That doesn't work:

org.springframework.beans.NullValueInNestedPathException: Invalid property 'map[TestKey][0]' 

NOTE According to this (Binding a map of lists in Spring MVC), this will be a problem because sub-objects are not auto-growable, and I need to implement a LazyList or a growable list? An ArrayList by itself is growable for Spring MVC's forms, but when used as a sub-collection, it's not? Very tricky.

Community
  • 1
  • 1
gene b.
  • 10,512
  • 21
  • 115
  • 227

1 Answers1

0

I solved this problem by following the LazyList/LazySet implementation in the link above, Binding a map of lists in Spring MVC

From their example,

public class PrsData {

  private Map<String, List<PrsCDData>> prsCDData;

  public PrsData() {
      this.prsCDData = MapUtils.lazyMap(new HashMap<String,List<Object>>(), new Factory() {

          public Object create() {
              return LazyList.decorate(new ArrayList<PrsCDData>(), 
                             FactoryUtils.instantiateFactory(PrsCDData.class));
          }

      });
  }

}

Once I had a data structure like that mapped to my JSP Path, the field binding magically started working and I could then make my path e.g. map["key"][index] in the JSP.

As they replied, SpringMVC auto-converts simple lists and maps to Lazy collections, but does not do it when the collection is a subobject.

Community
  • 1
  • 1
gene b.
  • 10,512
  • 21
  • 115
  • 227