技术开发 频道

在Tapestry中通用的property selection model


IT168技术文档】 
    Tapestry中构建选择列表需要使用IPropertySelectionModel,Model可以映射选择列表中的Label和Option,Tapestry中已经提供一个常用的StringSelectonModel,具体组件使用如下: 

    Html代码 

    java 代码
"@PropertySelection" model="ognl:@com.myexample.DetailsPage@GENDER_MODEL"
value
="ognl:gender"/>
    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。
0
相关文章