Dynamic list binding in Spring MVC

本文介绍如何在Spring MVC中处理包含依赖对象列表的复杂模型。通过使用commons-collections包中的LazyList类来简化操作,并详细解释了实现过程中的关键粘合点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值