网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 简单扩展让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.可以自行开发一个
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/17 13:23:42