Tapestry中建構選擇清單需要使用IPropertySelectionModel,Model可以映射選擇清單中的Label和Option,Tapestry中已經提供一個常用的StringSelectonModel,具體元件使用如下:<o:p></o:p>
Html代碼<o:p></o:p>
Java代碼
java 代碼
- public abstract class DetailsPage extends BasePage {
- public static final IPropertySelectionModel GENDER_MODEL =
- new StringPropertySelectionModel(new String[] { "Unspecified", "Female", "Male" });
- public abstract String getGender();
- public void formSubmit(IRequestCycle cycle) {
- // Process form submission
- String genderSelection = getGender();
- ...
- }
- }
開發中比較常見的選擇清單就是類别的選擇,,一般是建立一個類别的SelectionModel,例如如下代碼
java 代碼
- public class TypeSelectionModel implements IPropertySelectionModel, Serializable {
- private List TypeList;
- public ItemSelectionModel(List typeList) {
- this. typeList = typeList;
- }
- public int getOptionCount() { return typeList.size(); }
- public Object getOption(int index) {
- return ((Type)typeList.get(index)).getId();
- }
- public String getLabel(int index) {
- return ((Type) typeList.get(index)).getName();
- }
- public String getValue(int index) { return Integer.toString(index); }
- public Object translateValue(String value) {
- return getOption(Integer.parseInt(value));
- }
- }
這隻是傳統方法,開發中的類别如果多了,每一個都要建立一個SelectionModel。
下面是在Tapestry JumpStart裡看到的通用的SelectionModel,感覺比較不錯,現介紹給大家,具體代碼如下:
java 代碼
- package com.javaeye.tapestrying.selectionModel;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.tapestry.form.IPropertySelectionModel;
- public class ObjectPropertySelectionModel implements IPropertySelectionModel {
- private List _list;
- private Method _methodToGetLabel = null;
- private Method _methodToGetOption = null;
- private boolean _allowNoSelection = false;
- private String _noSelectionMessage = null;
- private int _offset = 0;
- public ObjectPropertySelectionModel() {
- _list = new ArrayList();
- throw new UnsupportedOperationException("Do not use the default constructor.");
- }
- public ObjectPropertySelectionModel(Listextends Object> list, Class clazz, String nameOfMethodToGetLabel,
- String nameOfMethodToGetOption) { this(list, clazz, nameOfMethodToGetLabel, nameOfMethodToGetOption, false, null);
- }
- public ObjectPropertySelectionModel(Listextends Object> list, Class clazz, String nameOfMethodToGetLabel,
- String nameOfMethodToGetOption, boolean allowNoSelection, String noSelectionLabel) {
- try {
- _list = list;
- _methodToGetLabel = clazz.getMethod(nameOfMethodToGetLabel, (Class[]) null);
- if (nameOfMethodToGetOption != null) {
- _methodToGetOption = clazz.getMethod(nameOfMethodToGetOption, (Class[]) null);
- }
- _allowNoSelection = allowNoSelection;
- if (_allowNoSelection) {
- _noSelectionMessage = noSelectionLabel;
- _offset = 1;
- }
- }
- catch (SecurityException e) {
- throw new IllegalStateException(e);
- }
- catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("Given nameOfMethodToGetLabel=\"" + nameOfMethodToGetLabel + "\", nameOfMethodToGetOption=\""
- + nameOfMethodToGetOption + "\", class=\"" + clazz + "\".", e);
- }
- }
- public int getOptionCount() {
- return _list.size() + _offset;
- }
- public Object getOption(int index) {
- if (_allowNoSelection) {
- if (index < _offset) {
- return null;
- }
- }
- Object o = _list.get(index - _offset);
- if (_methodToGetOption == null) {
- return o;
- }
- else {
- try {
- Object option = _methodToGetOption.invoke(o, (Object[]) null);
- return option;
- }
- catch (IllegalArgumentException e) {
- throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);
- }
- catch (IllegalAccessException e) {
- throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);
- }
- catch (InvocationTargetException e) {
- throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);
- }
- }
- }
- public String getLabel(int index) {
- if (_allowNoSelection) {
- if (index < _offset) {
- return _noSelectionMessage;
- }
- }
- Object o = _list.get(index - _offset);
- try {
- String label = _methodToGetLabel.invoke(o, (Object[]) null).toString();
- return label;
- }
- catch (IllegalArgumentException e) {
- throw new IllegalStateException("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);
- }
- catch (IllegalAccessException e) {
- throw new IllegalStateException("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);
- }
- catch (InvocationTargetException e) {
- throw new IllegalStateException ("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);
- }
- }
- public String getValue(int index) {
- return Integer.toString(index);
- }
- public Object translateValue(String value) {
- return value == null ? null : getOption(Integer.parseInt(value));
- }
- }
具體使用如下,假如現在有一個配件類型的類
java 代碼
- public class FittingType {
- private Long id;
- private String name;
- private Set<fitting></fitting> fittings = new HashSet<fitting></fitting>();
- //getter and setter
- }
<o:p></o:p>
建立配件類時,需要選擇配件類型
java 代碼
- private IPropertySelectionModel fittingTypeSelectionModel;
- public IPropertySelectionModel getFittingTypeSelectionModel() {
- if (fittingTypeSelectionModel == null) {
- List<fittingtype></fittingtype> fittingTypes = getFittingTypeService().findAll();
- fittingTypeSelectionModel = new ObjectPropertySelectionModel(
- fittingTypes, FittingType.class, "getName", "getId", true,
- "選擇分類");
- }
- return fittingTypeSelectionModel;
- }
- public abstract Long getFittingTypeId();
- public ILink onSave() {
- //其他
- if (getFittingTypeId() != null) {
- fitting.setType(getFittingTypeService()
- .findByID(getFittingTypeId()));
- }
- //其他
- }
Html中定義元件<o:p></o:p>
java 代碼
- "[email protected]" model="ognl:fittingTypeSelectionModel" value="ognl:fittingTypeId" displayName="配件類型"/>
<o:p> </o:p>
具體定義中
fittingTypeSelectionModel = new ObjectPropertySelectionModel(<o:p></o:p>
fittingTypes, FittingType.class, "getName", "getId", true,<o:p></o:p>
"選擇分類");<o:p></o:p>
}<o:p></o:p>
參數1:候選的List
參數2:類别
參數3:label的getter方法
參數4:option的getter方法
參數5:是否允許沒有選項
參數6:沒有選項時的label
<o:p> </o:p>
好了,項目中又多了個通用的東西,開發時又可以 happy 了。
java 代碼
- "@PropertySelection" model="ognl:@[email protected]_MODEL" value="ognl:gender"/>