簡單擴展讓beetl html標簽支持父子嵌套
來源:易賢網 閱讀:1227 次 日期:2016-06-27 11:07:15
溫馨提示:易賢網小編為您整理了“簡單擴展讓beetl html標簽支持父子嵌套”,方便廣大網友查閱!

默認情況下,beetl的html標簽并不支持父子嵌套,就像類似jsp標簽那樣,父標簽需要知道子標簽的信息,子標簽也需要知道父標簽信息。但是beetl只需要簡單擴展,就能完成嵌套標簽支持。

首先看一個最終的使用效果,實現倆個html標簽table.tag,tr.tag.可以在頁面上這么用:

<#table data =${userlist}>

<#tr class=3c name=name> 名稱 </#tr>

</#table>

在閱讀table.tag,tr.tag之前,先看看如何擴展html標簽

首先,需要擴展 htmltagsupportwrapper,這個類是html標簽實現類,我們可以擴展此類來定制化需求,然后重新注冊覆蓋。因此實現類

public class htmlnesttagsupportwrapper extends htmltagsupportwrapper{

public void render(){....}

}

然后在配置文件配置tag.htmltag= bingo.util.htmlnesttagsupportwrapper 就可以生效。

htmlnesttagsupportwrapper用到了tagnestcontext類,這個類其實就是一個樹形結構,記錄了parent的context,記錄了當前tag信息,以及記錄了子tag的context,這樣,每個tag都可以訪問父tag或者子tag:代碼如下

public class tagnestcontext {

private tag tag = null;

private tagnestcontext parent = null;

private list<tagnestcontext> children = null;

public tag gettag() {

return tag;

}

public void settag(tag para) {

this.tag = para;

}

public tagnestcontext getparent() {

return parent;

}

public void setparent(tagnestcontext parent) {

this.parent = parent;

}

public list<tagnestcontext> getchildren() {

if(children==null) children = new arraylist<tagnestcontext>();

return children;

}

public void setchildren(list<tagnestcontext> children) {

this.children = children;

}

}

回頭在看看 htmlnesttagsupportwrapper實現

public void render()

{

httpservletrequest request = (httpservletrequest)this.ctx.getglobal(request);

tagnestcontext tnc = (tagnestcontext)request.getattribute(tagcontext);

if(tnc==null){

tnc = new tagnestcontext();

tnc.settag(this);

request.setattribute(tagcontext, tnc);

super.render();

request.removeattribute(tagcontext);

}else{

tagnestcontext child = new tagnestcontext();

child.setparent(tnc);

child.settag(this);

tnc.getchildren().add(child);

request.setattribute(tagcontext, child);

super.render();

//重新設置

request.setattribute(tagcontext, child.getparent());

}

}

public string gettagname(){

return (string)this.args[0];

}

public object get(string attr){

map map = (map)this.args[1];

return map.get(attr);

}

如上代碼所示,但渲染某個htmltag前(調用super.render()前),可以從request里獲取nestcontext,如果沒有,生成一個新的nestcontext。如果已經存在。則將當前nestcontext加入到父nestcontext。渲染完畢后,需要重置nestcontext。

最后看一下tr.tag如何實現,tr僅僅實現了生成表頭

<tr class=${class}>${tagbody}</tr>

table.tag 則要麻煩點,需要知道有多少個tr,然后輸出數據,內容如下:

<table>

${tagbody}

<% for(var item in data){%>

<tr>

<%

var tag = gettagcontext();

var children = tag.children;

for(var tdtagctx in children){

print(<td>);

var tdtag = tdtagctx.tag;

var name = tdtag.tagname;

var value = item[name];

print(value);

println(</td>);

}

%>

</tr>

<%} %>

</table>

ettagcontext 是一個注冊方法,獲取當前context.可以自行開發一個

更多信息請查看網頁制作
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
未满十八18勿进黄网站免费看