JSP中的Simple自定义标签使用

来源:互联网 发布:农村淘宝现状分析 编辑:程序博客网 时间:2024/05/18 03:02

Simple自定义标签使用

今天我们主要讲一下我们的simple这个自定义标签的使用!首先我们看一下simple这个是怎么使用的。

我们先看一下java类,首先java类要继承SimpleTagSupport类。

第一个实例:我们是输出那个自定义标签的内容。

我们重写doTag方法 这样写JspFragment jf=this.getJspBody();我们就获取到了jf这个被容的对象,然后我们用jf.invoke(null);就能输出了,这里的null不是输出是空的。

第二个实例:获取属性

在对应的类中写到的属性名称要和你在自定义方法中定义的一样。而且我们还得写一个setter方法。一定要按正确的格式。下面给一个例子 是一个count的例子   <tag>

       <description>display user ip</description>

       <name>Demo8</name>

       <tag-class>com.web.tag.Demo8</tag-class>

       <body-content>scriptless</body-content>

        <attribute>

       <name>count</name>

       <required>yes</required>

       <rtexprvalue>yes</rtexprvalue>

       </attribute>

   </tag>

public class Demo8 extends SimpleTagSupport{

  privateint count;

 

  publicvoid setCount(int count) {

       this.count= count;

  }

 

  @Override

  publicvoid doTag() throws JspException, IOException {

       JspFragmentjf=this.getJspBody();

      

       for(int i = 0; i < count; i++) {

             jf.invoke(null);

       }

      

  }

  <hbsi:Demo8count="5" >

   This is my JSP page. <br>

   </hbsi:Demo8>

这就是重复5次  如果在多name我们更改就行了

实例三:大小写转换

 <hbsi:Demo9>

   This is my JSP page. <br>

   </hbsi:Demo9>

 

  publicvoid doTag() throws JspException, IOException {

       JspFragmentjf=this.getJspBody();

       StringWritersw=new StringWriter();

       jf.invoke(sw);

       Stringcontent=sw.toString().toUpperCase();

       JspWriterjw=this.getJspContext().getOut();

       jw.write(content);

  }

这就变成了大写的了

原创粉丝点击