默认情况下,Beetl的html标签并不支持父子嵌套,就像类似jsp标签那样,父标签需要知道子标签的信息,子标签也需要知道父标签信息。但是beetl只需要简单扩展,就能完成嵌套标签支持。
首先看一个最终的使用效果,实现俩个html标签table.tag,tr.tag.可以在页面上这么用:
立即学习“前端免费学习笔记(深入)”;
<#table data ="${userList}"><#tr class="3c" name="name"> 名称 </#tr></#table>
立即学习“前端免费学习笔记(深入)”;
public class HTMLNestTagSupportWrapper extends HTMLTagSupportWrapper{public void render(){....}}
立即学习“前端免费学习笔记(深入)”;
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;}}
立即学习“前端免费学习笔记(深入)”;
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);}
立即学习“前端免费学习笔记(深入)”;
<tr class="${class}">${tagBody}</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>
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号