How to bind form field to a Map of Objects in Play 2.03

1k views Asked by At

Trying to bind a field to a map of objects I got this error "*[NullValueInNestedPathException: Invalid property 'wrappedText[index]' of bean class [models.Simple]: Could not determine property type for auto-growing a default value]

From what I've read the autogrow has to do with attempting to populate the Map on the fly..but that's about as much as i've gotten..

I also came across this question which asks virtually the same question but in the context of Spring MVC ,I see that the accepted answer seems to be suggesting that the OP implement their own map what is the reasoning behind this and is there an alternative?

Models:

public class Simple {

  public String text;
  public List<String> stringList;
  public TreeMap<String,SimpleWrapper> wrappedText=new TreeMap<String,SimpleWrapper>();
}

...

public  class SimpleWrapper {

    String singleString;

    public void setSingleString(String singleString){
      this.singleString=singleString;
    }
    public String getSingleString(){
      return singleString;
    }
    public SimpleWrapper(){
      this.singleString=singleString;
    }

Controller

static Form<Simple> simpleform=form(Simple.class);
public static Result simpleForm(){

    Form<Simple> filledForm=simpleform.bindFromRequest();
    System.out.println(filledForm);

    return ok(views.html.simpleForm.render(filledForm.get().toString()));
}

View

@(text:String)
@import helper._

@form(routes.Management.simpleForm()){
<input type="hidden" value="string" name="stringList[0]">
<input type="hidden" value="string Again" name="stringList[1]">
<input type="hidden" value="mapp value" name="wrappedText[index].singleString">
<input type="text" id="text" name="text">
<input type="submit" value="submit">
}

This was passed @text

1

There are 1 answers

0
jgatcher On BEST ANSWER

Ok so I guess I misunderstood the answer to the other Question.I did not have to implement a LazyMap since the guys at apache-commons have already done that.

I ended up modifying my model to this:

Model

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.map.LazySortedMap;


public class Simple {
   public String text;
   public List<String> stringList;
   Factory<SimpleWrapper> factory = new Factory() {
       public SimpleWrapper create() {
          return new SimpleWrapper();
       }
   };

   public Map<String,SimpleWrapper> wrappedText =LazySortedMap.decorate(new TreeMap<String,SimpleWrapper>(),factory);
}