Dynamic list binding in Spring MVC

The Spring MVC documentation just isn't quite there. It is pretty basic, and doesn't really help with some common medium difficulty scenarios. The one I am documenting today is how to take a typical model (with lists of dependent objects), show it in a form, and get the graph back upon submission...

Rather than go through the entire thought process from beginning to end, I am going to show the end state and then explain the major glue points that make everything work. The assumption is that you have an ok understanding of Spring MVC.

The model

Here's a simple model for the illustration. There is an object named Grid which has a list of Block objects..

public class Grid {
  private List blocks = 
    LazyList.decorate(
      new ArrayList(),
      FactoryUtils.instantiateFactory(Block.class));

  public List getBlocks() {
    return blocks;
  }

  public void setBlocks(List list) {
    blocks = list;
  }
}



the LazyList class is the key here. This is in from the commons-collections package. I'll talk about why this is key later.

public class Block {
  private String id, description;

  public String getDescription() {
    return description;
  }

  public String getId() {
    return id;
  }

  public void setDescription(String string) {
    description = string;
  }

  public void setId(String string) {
    id = string;
  }
}

你可能感兴趣的:(spring,mvc,Go)