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

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

 

标题 jsp自定义标签之ifelse与遍历自定义标签示例
内容
    这篇文章主要介绍了jsp自定义标签之ifelse与遍历自定义标签,需要的朋友可以参考下
    第一个示例:
    简单的jsp自定标签获取内容:
    首先创建一个jsp实例类然后继承simpletagsupport类
    然后实现父类的dotag()方法
    在这个方法里获取标签体里的内容this.getjspbody();
    返回的是jspfragment 类,根据这个类对象调用invoke(this.getjspcontext().getout());
    这个方法里面也可以写空,所表达的意思也是输出到浏览器;
    代码如下:
    public class simpledmeo1 extends simpletagsupport {
    @override
    public void dotag() throws jspexception, ioexception {
    jspfragment js =this.getjspbody();
    js.invoke(null);
    }
    }
    然后在写tld文件标签库描述文件,和jsp文件,这些都较为简单
    如果不想执行某个内容就抛出异常
    throw new skippageexception();和面内容就不会显示
    接下来是一个带属性的jsp自定义标签文件
    代码如下:
    public class simpledmeo1 extends simpletagsupport {
    private int counts;
    public void setcounts(int counts) {
    this.counts = counts;
    }
    @override
    public void dotag() throws jspexception, ioexception {
    jspfragment js =this.getjspbody();
    for(int i=0;i<counts;i++){ //循环获取
    js.invoke(null);
    }
    }
    }
    <description>a tag library exercising simpletag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>c</short-name>前缀名
    <uri>http://www.csdn.com</uri>
    <tag>
    <name>demo</name>
    <tag-class>com.csdn.simple.simpledmeo1</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
    <name>counts</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
    然后再jsp文件 中写出内容;
    代码如下:
    <hbsi:demo counts=3>aaaaaaa<br/></hbsi:demo> //输出三编
    jspfragment js = this.getjspbody();
    stringwriter jw = new stringwriter();
    js.invoke(jw);
    string s = jw.tostring().touppercase();
    jspwriter out =this.getjspcontext().getout();
    for(int i=0;i<counts2;i++){
    out.print(s);
    }
    }
    这是转成大写的代码,其他的都一致;
    关于if else的代码,太多,我放到资源里了,有必要的话可以下载下来,仅供参考。
随便看

 

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

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/16 8:08:25