- tapestry
- 等级:
- 文章: 83
- 积分: 184
|
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) {
-
-
-
- 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>();
-
- }
<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 代码
- "type@PropertySelection" 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:@com.myexample.DetailsPage@GENDER_MODEL" value="ognl:gender"/>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- tapestry
- 等级:
- 文章: 83
- 积分: 184
|
改了n遍终于提交上来了,请问html代码如何加,加上去之后自动解析,后边的都没有了,上边的是去掉了好多html代码后的,少了一些内容。
|
返回顶楼 |
|
|
- cgc520
- 等级: 初级会员
- 性别:
- 文章: 5
- 积分: 30
- 来自: 广州
|
错是不错,可是我总觉得效率还是太低了,因为它用到了java的一些反射机制.页面刷新的时候可能会慢点吧!
|
返回顶楼 |
|
|