beetl模板引擎之自定义html标签,Beetl模板引擎自定义分页标签
1)在beetl.properties文件中配置tag
TAG.page = xx.xx.xx.xx.PageTag
2)在模板中引用標簽
3)增加標簽類繼承自GeneralVarTagBinding類
import org.beetl.core.Context;
import org.beetl.core.GeneralVarTagBinding;
import org.beetl.core.statement.Statement;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class PageTag extends GeneralVarTagBinding {
private int pageCount;
private int current;
@Override
public void init(Context ctx, Object[] args, Statement st) {
super.init(ctx, args, st);
Map pageMap = new HashMap<>();
Map val = null;
try {
val = (Map) getAttributeValue("val");
} catch (Exception e) {
}
if (val != null) {
pageMap.putAll(val);
}
if (pageMap.get("pageCount") != null) {
pageCount = pageMap.get("pageCount");
}
if (pageMap.get("current") != null) {
current = pageMap.get("current");
}
if (current < 1) current = 1;
}
@Override
public void render() {
StringBuilder obj = new StringBuilder();
//上一頁
if (current > 1) {
obj.append("上一頁");
} else {
obj.append("上一頁");
}
//中間頁碼
if (current != 1 && current >= 4 && pageCount != 4) {
obj.append("" + 1 + "");
}
if (current - 2 > 2 && current <= pageCount && pageCount > 5) {
obj.append("...");
}
int start = current - 2, end = current + 2;
if ((start > 1 && current < 4) || current == 1) {
end++;
}
if (current > pageCount - 4 && current >= pageCount) {
start--;
}
for (; start <= end; start++) {
if (start <= pageCount && start >= 1) {
if (start != current) {
obj.append("" + start + "");
} else {
obj.append("" + start + "");
}
}
}
if (current + 2 < pageCount - 1 && current >= 1 && pageCount > 5) {
obj.append("...");
}
if (current != pageCount && current < pageCount - 2 && pageCount != 4) {
obj.append("" + pageCount + "");
}
//下一頁
if (current < pageCount) {
obj.append("下一頁");
} else {
obj.append("下一頁");
}
try{
ctx.byteWriter.writeString(obj.toString());
this.doBodyRender();
}catch (IOException e){
}
}
}
總結
以上是生活随笔為你收集整理的beetl模板引擎之自定义html标签,Beetl模板引擎自定义分页标签的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自先沉稳而后爱人什么意思 自先沉稳而后爱
- 下一篇: 出去浪是什么意思 词语出去浪是什么意思