Posted on 2010-03-06 15:54
云自无心水自闲 阅读(2240)
评论(0) 编辑 收藏 所属分类:
Java 、
心得体会 、
Tapestry
在教程的第四部分(http://www.blogjava.net/usherlight/archive/2009/06/20/283396.html)我们已经粗略地介绍了Tapestry的前缀,其中提及最常用的两种literal和prop。这里顺便再介绍一下其他的几个前缀.
1、context,这个经常在引用图片的时候用到。比如:<img src="${context:images/icon.png}"/> 这样就能够在页面上显示在web-inf/images/icon.png
2、message,这个是需要I18N的应用必用的前缀。${message:some-key}
3、var,用于定义生成一个临时变量。比如:<li t:type="loop" source="1..10" value="var:index">${var:index}</li>。如果没有加这个前缀var:的话,就需要在页面类中添加一个property: index以及相应的getter和setter。
我们这次介绍自定义一个cycle前缀,这个可以用于在表格中显示不同背景的行(也就是斑马条)。
比如:
<t:grid .... rowClass="cycle:line1,line2">
...
</t:grid>
或者
<t:loop ...>
<div class="${cycle:line1,line2}">aaaa</div>
</tloop>
自定义prefix一般来说是3个步骤,
1、定义一个BindingFactory,这个需要实现BindingFactory接口
2、定义一个Binding继承AbstractBinding
3、注册这个Binding
看一下具体的prefix的类:
1import java.util.ArrayList;
2import java.util.List;
3
4import org.apache.tapestry5.Binding;
5import org.apache.tapestry5.BindingConstants;
6import org.apache.tapestry5.ComponentResources;
7import org.apache.tapestry5.ioc.Location;
8import org.apache.tapestry5.services.BindingFactory;
9import org.apache.tapestry5.services.BindingSource;
10
11/** *//**
12 * Implementation of the cycle: binding prefix -- we parse list of bindings
13 * and generate delegate bindings for each element<br>
14 * default binding is literal, other bindings can be used by specifying prefix.<br>
15 * example: "cycle:prop:name,prop:lastName,sth,sth else"
16 */
17public class CycleBindingFactory implements BindingFactory {
18 private final BindingSource _bindingSource;
19
20 public CycleBindingFactory(BindingSource source){
21 this._bindingSource = source;
22 }
23
24 public Binding newBinding(String description, ComponentResources container, ComponentResources component,
25 String expression, Location location)
26 {
27 List<Binding> delegates = new ArrayList<Binding>();
28 String[] bindingNames = expression.split(",");
29
30 for (String bindingName : bindingNames){
31 String defaultBinding = BindingConstants.LITERAL;
32
33 Binding binding = _bindingSource.newBinding(description, container, component, defaultBinding, bindingName, location);
34 delegates.add(binding);
35 }
36
37 CycleBinding cycleBinding = new CycleBinding(delegates);
38 container.addPageLifecycleListener(cycleBinding);
39
40 return cycleBinding;
41 }
42}
1import java.util.List;
2
3import org.apache.tapestry5.Binding;
4import org.apache.tapestry5.internal.bindings.AbstractBinding;
5import org.apache.tapestry5.runtime.PageLifecycleListener;
6
7
8public class CycleBinding extends AbstractBinding implements PageLifecycleListener{
9 private final List<Binding> delegates;
10 private int index = 0;
11
12 public CycleBinding(List<Binding> delegates) {
13 this.delegates = delegates;
14 }
15
16 public Object get() {
17 Object ret = delegates.get(index).get();
18 index ++;
19 if(index>=delegates.size()) index = 0;
20 return ret;
21 }
22
23 @Override
24 public boolean isInvariant() {
25 return false;
26 }
27
28 @Override
29 public Class<Object> getBindingType() {
30 return Object.class;
31 }
32
33
34 public void containingPageDidDetach() {
35 index=0;
36 }
37
38 public void containingPageDidAttach() {/**//*not interested*/}
39
40 public void containingPageDidLoad() {/**//*not interested*/}
41}
Binding和BindingFactory写好了,注册后就可以使用了,注册的过程是在AppModel中添加以下一段代码:
1 public static void contributeBindingSource(
2 MappedConfiguration<String, BindingFactory> configuration,
3 BindingSource bindingSource
4 )
5 {
6 configuration.add("cycle",new CycleBindingFactory(bindingSource));
7 }