TagSupport继承

下面还是开发一个HelloWorld的标签程序,只是这个标签程序直接从TagSupport类继承。

package com.test.ch13;
.....

public class HelloTag extends TagSupport
{
/** 覆盖doStartTag方法*/
public int doStartTag() throws JspTagException{
return EVAL_BODY_INCLUDE;
}
/** 覆盖doEndTag方法*/
public int doEndTag()throws JspTagException{

String dateString = new Date().toString();
try{
pageContext.getOut().write("Hello World helking.<br>现在时间是:"+dateString);
}
catch(IOException ex){
throw new JspTagException("Fatal error:hello tag conld not write to JSP out");
}
return EVAL_PAGE;
}
}

在上面的代码中,由于从TagSupport类继承,只覆盖了两个方法就开发出了标签,说明用这种方式开发标签程序还是比较简单的。
由于可以在一个tld文件中描述多个标签,故可以在前面的mytag.tld文件中添加以下的描述

<tag>
<name>hello</name>
<tag-class>com.test.ch13.HelloTag</tag-class>
<body-content>empty</body-content>
<description>
Simple hello world examples.
Takes no attribute, and simply generates HTML
</description>
</tag>

同时编写一个测试的JSP的主要内容:

<%@ taglib uri="/demotag" prefix="hello"%>
<%@ page contentType="text/html;charset=gb2312" language="java%>
......
<p> 以下是显示Taglib内容:</p>
<p><i><hello:hello/></i></p>
</body>
</html>

你可以参考然后试试。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值