在教程的
第四部分我们已经粗略地介绍了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的类:
CycleBindingFactory.java
import java.util.ArrayList;
import java.util.List;
import org.apache.tapestry5.Binding;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.ioc.Location;
import org.apache.tapestry5.services.BindingFactory;
import org.apache.tapestry5.services.BindingSource;
/** *//**
* Implementation of the cycle: binding prefix -- we parse list of bindings
* and generate delegate bindings for each element<br>
* default binding is literal, other bindings can be used by specifying prefix.<br>
* example: "cycle:prop:name,prop:lastName,sth,sth else"
*/
public class CycleBindingFactory implements BindingFactory {
private final BindingSource _bindingSource;
public CycleBindingFactory(BindingSource source){
this._bindingSource = source;
}
public Binding newBinding(String description, ComponentResources container, ComponentResources component,
String expression, Location location) {
List<Binding> delegates = new ArrayList<Binding>();
String[] bindingNames = expression.split(",");
for (String bindingName : bindingNames){
String defaultBinding = BindingConstants.LITERAL;
Binding binding = _bindingSource.newBinding(description, container, component, defaultBinding, bindingName, location);
delegates.add(binding);
}
CycleBinding cycleBinding = new CycleBinding(delegates);
container.addPageLifecycleListener(cycleBinding);
return cycleBinding;
}
}
CycleBinding.java
import java.util.List;
import org.apache.tapestry5.Binding;
import org.apache.tapestry5.internal.bindings.AbstractBinding;
import org.apache.tapestry5.runtime.PageLifecycleListener;
public class CycleBinding extends AbstractBinding implements PageLifecycleListener {
private final List<Binding> delegates;
private int index = 0;
public CycleBinding(List<Binding> delegates) {
this.delegates = delegates;
}
public Object get() {
Object ret = delegates.get(index).get();
index ++;
if(index>=delegates.size()) index = 0;
return ret;
}
@Override
public boolean isInvariant() {
return false;
}
@Override
public Class<Object> getBindingType() {
return Object.class;
}
public void containingPageDidDetach() {
index=0;
}
public void containingPageDidAttach() {/**//*not interested*/}
public void containingPageDidLoad() {/**//*not interested*/}
}
Binding和BindingFactory写好了,注册后就可以使用了,注册的过程是在AppModel中添加以下一段代码:
public static void contributeBindingSource(
MappedConfiguration<String, BindingFactory> configuration,
BindingSource bindingSource) {
configuration.add("cycle",new CycleBindingFactory(bindingSource));
}
原文地址:http://www.blogjava.net/usherlight/archive/2010/03/06/314700.html