时间:2021-07-01 10:21:17 帮助过:118人阅读
首先看一个最终的使用效果,实现俩个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 Listchildren = 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;}}
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);}
${tagBody}
"); var tdTag = tdTagCtx.tag; var name = tdTag.tagName; var value = item[name]; print(value); println(" | ");}%>