简单扩展让beetl HTML标签支持父子嵌套_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:49:31
Original
1340 people have browsed it

默认情况下,Beetl的html标签并不支持父子嵌套,就像类似jsp标签那样,父标签需要知道子标签的信息,子标签也需要知道父标签信息。但是beetl只需要简单扩展,就能完成嵌套标签支持。

首先看一个最终的使用效果,实现俩个html标签table.tag,tr.tag.可以在页面上这么用:

<#table data ="${userList}"><#tr class="3c" name="name"> 名称 
Copy after login



在阅读table.tag,tr.tag之前,先看看如何扩展html标签
首先,需要扩展 HTMLTagSupportWrapper,这个类是html标签实现类,我们可以扩展此类来定制化需求,然后重新注册覆盖。因此实现类

public class HTMLNestTagSupportWrapper extends HTMLTagSupportWrapper{public void render(){....}}
Copy after login



然后在配置文件配置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 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 getChildren() { if(children==null) children = new ArrayList(); return children;}public void setChildren(List children) { this.children = children;}}
Copy after login



回头在看看 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);}
Copy after login



如上代码所示,但渲染某个htmltag前(调用super.render()前),可以从request里获取NestContext,如果没有,生成一个新的NestContext。如果已经存在。则将当前NestContext加入到父NestContext。渲染完毕后,需要重置NestContext。

最后看一下tr.tag如何实现,tr仅仅实现了生成表头

${tagBody}
Copy after login



table.tag 则要麻烦点,需要知道有多少个tr,然后输出数据,内容如下:

${tagBody}<% for(var item in data){%><%var tag = getTagContext();var children = tag.children;for(var tdTagCtx in children){ print("");}%><%} %>
"); var tdTag = tdTagCtx.tag; var name = tdTag.tagName; var value = item[name]; print(value); println("
Copy after login



etTagContext 是一个注册方法,获取当前context.可以自行开发一个
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!