---
title: "Tapestry Components 通用PropertySelection "
author: "topgun"
date: "2006-11-16T18:41:03Z"
tags: []
slug: "tapestry-components-propertyselection"
draft: false
lastmod: "2006-11-16T18:41:03Z"
id: 34631
url: "https://topgun.iteye.com/blog/34631"
---

# 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 为属性选择模型&lt;component id="deptSelect" type="PropertySelection"&gt;        &lt;binding name="value" value="department"&gt;&lt;/binding&gt;  
        &lt;binding name="model" value="availDepts"&gt;&lt;/binding&gt;  
&lt;/component&gt;

## &lt;component id="deptSelect" type="PropertySelection"&gt;       &lt;binding name="value" value="department"/&gt;       &lt;binding name="model" value="availDepts"/&gt;   &lt;/component&gt;   PropertySelection 组件设定默认值

例子：

在.page定义了如下组件：

```
<component id="departmentSelect" type="PropertySelection">
xml 代码

<component id="departmentSelect" type="PropertySelection">  <binding name="value" value="department"/>  <binding name="model" value="availDepts"/>  </component>  
<binding name="value" value="department"></binding>
<binding name="model" value="availDepts"></binding>
</component>		 
				
```

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

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