天天看點

在Tapestry中通用的property selection model

     Tapestry中建構選擇清單需要使用IPropertySelectionModel,Model可以映射選擇清單中的Label和Option,Tapestry中已經提供一個常用的StringSelectonModel,具體元件使用如下:<o:p></o:p>

Html代碼<o:p></o:p>

Java代碼

java 代碼  

  1. public abstract class DetailsPage extends BasePage {  
  2.   public static final IPropertySelectionModel GENDER_MODEL =  
  3.       new StringPropertySelectionModel(new String[] { "Unspecified", "Female", "Male" });  
  4.   public abstract String getGender();  
  5.   public void formSubmit(IRequestCycle cycle) {  
  6.       // Process form submission  
  7.       String genderSelection = getGender();  
  8.       ...  
  9.   }  
  10. }  

      開發中比較常見的選擇清單就是類别的選擇,,一般是建立一個類别的SelectionModel,例如如下代碼

java 代碼  

  1. public class TypeSelectionModel implements IPropertySelectionModel, Serializable {  
  2.   private List TypeList;  
  3.   public ItemSelectionModel(List typeList) {  
  4.       this. typeList = typeList;  
  5.   }  
  6.   public int getOptionCount() { return typeList.size(); }  
  7.   public Object getOption(int index) {  
  8.       return ((Type)typeList.get(index)).getId();  
  9.   }  
  10.   public String getLabel(int index) {  
  11.       return ((Type) typeList.get(index)).getName();  
  12.   }  
  13.   public String getValue(int index) { return Integer.toString(index); }  
  14.   public Object translateValue(String value) {  
  15.       return getOption(Integer.parseInt(value));  
  16.   }  
  17. }  

這隻是傳統方法,開發中的類别如果多了,每一個都要建立一個SelectionModel。

下面是在Tapestry JumpStart裡看到的通用的SelectionModel,感覺比較不錯,現介紹給大家,具體代碼如下:

java 代碼  

  1. package com.javaeye.tapestrying.selectionModel;  
  2. import java.lang.reflect.InvocationTargetException;   
  3. import java.lang.reflect.Method;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.apache.tapestry.form.IPropertySelectionModel;  
  7. public class ObjectPropertySelectionModel implements IPropertySelectionModel {  
  8.        private List _list;  
  9.        private Method _methodToGetLabel = null;  
  10.        private Method _methodToGetOption = null;  
  11.        private boolean _allowNoSelection = false;  
  12.        private String _noSelectionMessage = null;  
  13.        private int _offset = 0;  
  14.        public ObjectPropertySelectionModel() {  
  15.               _list = new ArrayList();  
  16.               throw new UnsupportedOperationException("Do not use the default constructor.");  
  17.        }  
  18.        public ObjectPropertySelectionModel(Listextends Object> list, Class clazz, String nameOfMethodToGetLabel,  
  19.                      String nameOfMethodToGetOption) {              this(list, clazz, nameOfMethodToGetLabel, nameOfMethodToGetOption, false, null);  
  20.        }  
  21.        public ObjectPropertySelectionModel(Listextends Object> list, Class clazz, String nameOfMethodToGetLabel,  
  22.                      String nameOfMethodToGetOption, boolean allowNoSelection, String noSelectionLabel) {  
  23.               try {  
  24.                      _list = list;  
  25.                      _methodToGetLabel = clazz.getMethod(nameOfMethodToGetLabel, (Class[]) null);  
  26.                      if (nameOfMethodToGetOption != null) {  
  27.                             _methodToGetOption = clazz.getMethod(nameOfMethodToGetOption, (Class[]) null);  
  28.                      }  
  29.                      _allowNoSelection = allowNoSelection;  
  30.                      if (_allowNoSelection) {  
  31.                             _noSelectionMessage = noSelectionLabel; 
  1.                        _offset = 1;  
  2.                      }  
  3.               }   
  4.               catch (SecurityException e) {   
  5.                      throw new IllegalStateException(e);  
  6.               }  
  7.               catch (NoSuchMethodException e) {   
  8.                     throw new IllegalArgumentException("Given nameOfMethodToGetLabel=\"" + nameOfMethodToGetLabel + "\", nameOfMethodToGetOption=\""  
  9.                             + nameOfMethodToGetOption + "\", class=\"" + clazz + "\".", e);  
  10.               }  
  11.        }  
  12.        public int getOptionCount() {    
  13.               return _list.size() + _offset;  
  14.        }    
  15.        public Object getOption(int index) {   
  16.               if (_allowNoSelection) {  
  17.                      if (index < _offset) {  
  18.                             return null;   
  19.                      }    
  20.               }  
  21.               Object o = _list.get(index - _offset);  
  22.               if (_methodToGetOption == null) {  
  23.                      return o;  
  24.               }  
  25.               else {  
  26.                      try {  
  27.                             Object option = _methodToGetOption.invoke(o, (Object[]) null);  
  28.                             return option;  
  29.                      }  
  30.                      catch (IllegalArgumentException e) {    
  31.                             throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);  
  32.                      }  
  33.                      catch (IllegalAccessException e) {  
  34.                             throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);  
  35.                      }  
  36.                      catch (InvocationTargetException e) {  
  37.                             throw new IllegalStateException("Problem with the given methodToGetOption \"" + _methodToGetOption + "\": ", e);  
  38.                      }  
  39.               }  
  40.        }
  41.        public String getLabel(int index) {  
  42.               if (_allowNoSelection) {  
  43.                      if (index < _offset) {  
  44.                             return _noSelectionMessage;
  45.                      }  
  46.               }  
  47.               Object o = _list.get(index - _offset);  
  48.               try {  
  49.                      String label = _methodToGetLabel.invoke(o, (Object[]) null).toString();  
  50.                      return label;  
  51.               }  
  52.               catch (IllegalArgumentException e) {  
  53.                      throw new IllegalStateException("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);  
  54.               }  
  55.                 catch (IllegalAccessException e) {   
  56.                      throw new IllegalStateException("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);    
  57.               }  
  58.               catch (InvocationTargetException e) {  
  59.                      throw new IllegalStateException   ("Problem with the given methodToGetLabel \"" + _methodToGetOption + "\": ", e);   
  60.               }    
  61.        }    
  62.       public String getValue(int index) {   
  63.               return Integer.toString(index);    
  64.        }    
  65.        public Object translateValue(String value) {    
  66.               return value == null ? null : getOption(Integer.parseInt(value));   
  67.        }    
  68. }  

具體使用如下,假如現在有一個配件類型的類

java 代碼

  1. public class FittingType {   
  2.     private Long id;  
  3.     private String name;  
  4.     private Set<fitting></fitting> fittings = new HashSet<fitting></fitting>();    
  5. //getter and setter 
  6. }  

<o:p></o:p>

建立配件類時,需要選擇配件類型

java 代碼

  1. private IPropertySelectionModel fittingTypeSelectionModel;  
  2. public IPropertySelectionModel getFittingTypeSelectionModel() {  
  3.        if (fittingTypeSelectionModel == null) {  
  4.            List<fittingtype></fittingtype> fittingTypes = getFittingTypeService().findAll();  
  5.            fittingTypeSelectionModel = new ObjectPropertySelectionModel(  
  6.                   fittingTypes, FittingType.class, "getName", "getId", true,  
  7.                   "選擇分類");   
  8.        }   
  9.        return fittingTypeSelectionModel;  
  10.     }  
  11. public abstract Long getFittingTypeId();  
  12. public ILink onSave() {   
  13.        //其他   
  14.        if (getFittingTypeId() != null) {  
  15.            fitting.setType(getFittingTypeService()  
  16.                   .findByID(getFittingTypeId()));  
  17.        }  
  18.        //其他    
  19.     }  

Html中定義元件<o:p></o:p>

java 代碼

  1. "[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 代碼  

  1. "@PropertySelection" model="ognl:@[email protected]_MODEL" value="ognl:gender"/>