使用自定义标签只需3步:
1)继承Tag类(或GeneralVarTagBinding类)实现render方法;
2)注册标签:
BeetlViewFactory.groupTemplate.registerTag("page", PageTag.class);
3)使用标签:
<#page val="${pageData.page}">
……
#page>
Tag类的源码:
引用
public abstract class Tag
{
protected Object[] args = null;
protected GroupTemplate gt;
protected Context ctx;
protected ByteWriter bw;
protected Statement bs;
protected void doBodyRender()
{
bs.execute(ctx);
}
protected BodyContent getBodyContent()
{
ByteWriter writer = ctx.byteWriter;
ByteWriter tempWriter = ctx.byteWriter.getTempWriter();
ctx.byteWriter = tempWriter;
doBodyRender();
ctx.byteWriter = writer;
return tempWriter.getTempConent();
}
public abstract void render();
public void init(Context ctx, Object[] args, Statement st)
{
this.ctx = ctx;
this.bw = ctx.byteWriter;
this.gt = ctx.gt;
this.args = args;
this.bs = st;
}
}
其中args参数第一个是标签名,即你自定义的标签名称 第二个是个Map保存了自定义标签的属性名,如:
分页标签<#page val="${pageData.page}">, args={'tage', Map[val=page]}
ctx是模版上下文,可以取全局变量:ctx.getGloabl("名字");
Beetl还有一个更友好的抽象类GeneralVarTagBinding,它提供一个getAttributeValue以属性名获取标签的属性值
GeneralVarTagBinding源码:
引用
package org.beetl.core;
import java.util.Map;
/**
* 此类子类将可以作为html 标签使用,除了能实现bindVars来初始化绑定的变量外,允许render方法里通过调用
* 方法bing(name,value) 来绑定变量,如下是一个例子
*
* public class TestGeneralVarTagBinding extends GeneralVarTagBinding
{
public void render()
{
for (int i = 0; i < 5; i++)
{
this.bind("value", i);
this.doBodyRender();
}
}
}
此例子会循环渲染标签体5次,并且每次循环都会设置value的值,如下是模板内容
<#tag ; value>
${value}
#tag>
*/
public abstract class GeneralVarTagBinding extends Tag implements TagVarBinding
{
private Map
@Override
public Object[] bindVars()
{
// TODO Auto-generated method stub
return null;
}
public void mapName2Index(Map
{
name2Index = map;
}
public void bind(String name, Object value)
{
if (name2Index == null)
{
throw new RuntimeException();
}
Integer index = name2Index.get(name);
if (index == null)
{
throw new RuntimeException();
}
ctx.vars[index] = value;
}
public Object getAttributeValue(String attrName)
{
Map map = (Map) this.args[1];
return map.get(attrName);
}
public String getHtmlTagName()
{
return (String) this.args[0];
}
}
下面是我写的分页标签的实现:
引用
public class PageTag extends GeneralVarTagBinding {
private long total = 0;
private int perSize = 0;
private int curNo = 0;
private int pageNum = 0;
private static final String SPAN = "span";
private static final String A = "a";
private void init(){
Page page = (Page)getAttributeValue("val");
total = page.getTotalNum();
perSize = page.getPerSize();
curNo = page.getCurNo();
pageNum = (int)(total / perSize);
if(pageNum * perSize < total){
pageNum++;
}
}
@Override
public void render(){
init();
StringBuilder paging = new StringBuilder("");
paging.append("
");
if(curNo > 1){
paging.append(" ")
.append(" ");
if(curNo > 4){
paging.append("...");
}
if(curNo - 2 > 0){
paging.append("").append(curNo - 2).append("");
}
if(curNo - 1 > 0){
paging.append("").append(curNo - 1).append("");
}
}else if(curNo == 1){
paging.append(" ")
.append(" ");
}
paging.append("").append(curNo).append("");
if(curNo + 1 <= pageNum){
paging.append("").append(curNo + 1).append("");
}
if(curNo + 2 <= pageNum){
paging.append("").append(curNo + 2).append("");
}
if(pageNum - curNo > 2){
paging.append("...");
}
if(curNo >= pageNum){
paging.append(" ")
.append(" ");
} else{
paging.append(" ")
.append(" ");
}
paging.append("")
.append("共").append(pageNum).append("页").append(total).append("条记录 到第")
.append("页")
.append("
");
if(curNo > 1){
paging.append(" ")
.append(" ");
if(curNo > 4){
paging.append("...");
}
if(curNo - 2 > 0){
paging.append("").append(curNo - 2).append("");
}
if(curNo - 1 > 0){
paging.append("").append(curNo - 1).append("");
}
}else if(curNo == 1){
paging.append(" ")
.append(" ");
}
paging.append("").append(curNo).append("");
if(curNo + 1 <= pageNum){
paging.append("").append(curNo + 1).append("");
}
if(curNo + 2 <= pageNum){
paging.append("").append(curNo + 2).append("");
}
if(pageNum - curNo > 2){
paging.append("...");
}
if(curNo >= pageNum){
paging.append(" ")
.append(" ");
} else{
paging.append(" ")
.append(" ");
}
paging.append("")
.append("共").append(pageNum).append("页").append(total).append("条记录 到第")
.append("页")
.append("
try{
ctx.byteWriter.writeString(paging.toString());
this.doBodyRender();
ctx.byteWriter.writeString(paging.toString());
}catch (IOException e){
throw new RuntimeException(e);
}
}
}
doBodyRender()方法是输出标签内的HTML
最终效果
更详细的使用文档可以去Beetl在线文档上看 http://ibeetl.com/guide/#_%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%87%E7%AD%BE