Tapestry Components 通用PropertySelection

本文介绍了一个通用的PropertySelection组件实现方案,通过定义Selectable接口及其实现类,配合SelectableModel模型,实现了灵活的业务对象选择功能。

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

Tapestry Components

 

通用PropertySelection

示例代码:svn://portal/private/wen/tapestry/common-property-selection

  • 需要一个通用bo 的接口Selectable ,被选的对象要实现这个接口
  • 需要通用模型类 SelectableModel

实体建模时,如果一个bo 是另一个BO 的属性,界面上需要保存BO时需要参考选择一个bo,bo 就需要实现Selectable 接口

public class Department extends BaseObject implements Selectable {
}		
			

Selectable 接口如下:

public interface Selectable {
	public Long getId();
	public void setId(Long id);
	public String getName();
	public void setName(String name);
}			
			

在BO 操作的Form 页面上类,要有bo 对象(就是有getter setter)

	public abstract Selectable getDepartment();
	public abstract void setDepartment(Selectable department);	
			

bo 的待选内容怎样来的呢?要在BO 操作的Form 页面上类里做如下处理:

  • 定义一个IPropertySelectionModel 属性选择模型
  • 将bo 的列表放到这个模型中
  • 页面不存在了,可以将模型设为空
	private IPropertySelectionModel availDepts = null;

	public IPropertySelectionModel getAvailDepts() {
		if (null == availDepts) {
			availDepts = new SelectableModel(服务对象.getDepts());
		}
		return availDepts;
	}

	public void pageDetached(PageEvent pageEvent) {
		availDepts = null;
	}
			

这个模型基本是通用的,不用再自己写了,使用bo 列表new 出来就行了

public class SelectableModel implements IPropertySelectionModel, Serializable {

	private static final long serialVersionUID = 3528841813175496347L;
	protected List itemList;

	public SelectableModel(List list) {
		itemList = list;
	}

	public int getOptionCount() {
		return itemList.size();
	}

	public Object getOption(int index) {
		return itemList.get(index);
	}

	public String getLabel(int index) {
		return ((Selectable) itemList.get(index)).getName();
	}

	public String getValue(int index) {
		return Integer.toString(index);
	}

	public Object translateValue(String value) {
		return getOption(Integer.parseInt(value));
	}
}
			

最后在页面上定义组件,组件类型为PropertySelection,绑定两个参数,value 为bo,model 为属性选择模型<component id="deptSelect" type="PropertySelection">        <binding name="value" value="department"></binding>
        <binding name="model" value="availDepts"></binding>
</component>

  1. <component id="deptSelect" type="PropertySelection">  
  2.     <binding name="value" value="department"/>  
  3.     <binding name="model" value="availDepts"/>  
  4. </component>  
PropertySelection 组件设定默认值

例子:

在.page定义了如下组件:

<component id="departmentSelect" type="PropertySelection">
xml 代码
  1. <component id="departmentSelect" type="PropertySelection">  
  2. <binding name="value" value="department"/>  
  3. <binding name="model" value="availDepts"/>  
  4. </component>  

<binding name="value" value="department"></binding>
<binding name="model" value="availDepts"></binding>
</component>

只要在页面类中,设定department的初始值,该值就是选中的默认值。

原因:当该组件遍历'model' availDepts时,会使用public Object getOption(int index)方法读出列表,当 读出的对象与'value'中的值相等时,就选中该项。 哈哈..简单吧!:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值