`

BodyTagSupport例子

阅读更多
1.在WEB-INF下新建一个stag.tld文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag 
Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
        <tlibversion>1.0</tlibversion>
        <jspversion>1.1</jspversion>
        <shortname>tbl</shortname>
   <tag>
        <name>stag</name>
        <tagclass>test.MyTag</tagclass>
        <bodycontent>JSP</bodycontent>
        <info>To Convert in Upper case tag</info>
   </tag>
</taglib>

2.
package test;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

public class MyTag extends BodyTagSupport 
{
    public int doAfterBody() throws JspException {
      try {
         BodyContent bodycontent = getBodyContent();
         System.out.println("---------bodycontent---------:"+bodycontent);
         String body = bodycontent.getString();
         System.out.println("---------body---------:"+body);
         JspWriter out = bodycontent.getEnclosingWriter();
         System.out.println("---------out---------:"+out);
         if(body != null) {
            out.print(body.toUpperCase());
         }
      } catch(IOException ioe) {
         throw new JspException("Error:"+ioe.getMessage());
      }
      return SKIP_BODY;
  }

	@Override
	public int doEndTag() throws JspException {
		System.out.println("---------doEndTag()----------");
		return super.doEndTag();
	}

	@Override
	public void doInitBody() throws JspException {
		System.out.println("---------doInitBody()----------");
		super.doInitBody();
	}

	@Override
	public int doStartTag() throws JspException {
		System.out.println("---------doStartTag()----------");
		return super.doStartTag();
	}
}

3.
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/WEB-INF/stag.tld" prefix="tbl" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <tbl:stag>
		hello ! BodyTagSupport例子
	</tbl:stag>
  </body>
</html>



完成!
分享到:
评论
1 楼 elvis4139 2012-07-20  
[img][/img][url][/url][flash=200,200][/flash]
[b][/b][i][/i]
引用
[u][/u]

相关推荐

    利用BodyTagSupport创建带标签体的自定义标签

    利用BodyTagSupport创建带标签体的自定义标签

    struts2自定义标签案例详解

    其实,开发自定义标签并不需要Struts2的支持,一般情况下,只需要继承javax.servlet.jsp.tagext.BodyTagSupport类,重写doStartTag,doEndTag等方法即可。这里在实现自定义标签时,继承的2个类分别是org.apache....

    在jsp中使用自定义taglib

    一个tag就是一个普通的java类,它唯一特别之处是它必须继承TagSupport或者BodyTagSupport类。这两个类提供了一些方法,负责jsp页面和你编写的类之间的交互,例如输入,输出。而这两个类是由jsp容器提供的,无须开发...

    javax.servlet.jsp.tagext源码

    TagSupport SimpleTagSupport tagext javax servlet jsp Tag 标签

    自定义标签

    通过extends BodyTagSupport实现自定义标签的代码实例;通过implements Tag实现自定义标签的代码实例

    jsp java自定义标签 实现分页 当前位置标签

    通过BodyTagSupport实现了自定义的标签,主要实现功能为分页技术以及当前功能点所在位置。

    jsp自定义标签中的困扰

    JSP自定义标签过程中会出现许多的问题,尤其是没有相应课题的专门书籍的时候.我在通过继承BodyTagSupport类来做标签的时候。出现了许多问题,我把这些问题归纳起来,希望能帮助到某些人。

    可伪可不伪的分页标签

    import javax.servlet.jsp.tagext.BodyTagSupport; public class FenyeTag extends BodyTagSupport { private static final long serialVersionUID = 1L; private String html; private String exname; ...

    JSP自定义标签案例分析

    public class MyReferer extends BodyTagSupport { private String site; private String back; public String getSite() { return site; } public void setSite(String site) { this.site = site; } ...

    jsp通过自定义标签库实现数据列表显示的方法

    本文实例讲述了jsp通过自定义标签库实现数据列表显示的方法。分享给大家供大家参考,具体如下: 1. 定义标签库类 UserListTag.java ... import java.io....import javax.servlet.jsp.tagext.BodyTagSupport; impor

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、标签 ...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、标签 ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、标签 ...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、标签 ...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、...

    李兴华Java Web开发实战经典(高清版) Part2

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、...

    JSP/Servlet

    BodyTagSupport 類別 自訂標籤屬性 自訂動態標籤屬性 SimpleTagSupport 類別() Scripting Variable Tag File Tag File是從JSP 2.0開始新增的功能,主要的目的是讓撰寫JSP網頁的網頁設計人員即使在不懂Java...

    java web 视频、电子书、源码(李兴华老师出版)

    12.9、使用BodyTagSupport开发迭代输出 12.10、简单标签 12.11、DynamicAttributes接口 12.12、本章摘要 第13章 标准标签库(JSTL) 13.1、JSTL简介 13.2、安装JSTL 1.2 13.3、核心标签库 13.3.1、...

Global site tag (gtag.js) - Google Analytics