用SimpleTagSupport种开发自定义标签

用SimpleTagSupport类开发自定义标签

package com.hbsi.****.web;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTagTest extends SimpleTagSupport {

 private  int counts;
 
 
 public int getCount() {
  return counts;
 }


 public void setCount(int count) {
  this.counts = count;
 }


 @Override
 public void doTag() throws JspException, IOException {
  // TODO Auto-generated method stub
  JspWriter out=this.getJspContext().getOut();
  String str=invokeBody();
  for(int i=0;i<counts;i++){
   out.println(str.toUpperCase());
  }
  super.doTag();
 }


 private String invokeBody() {
  // TODO Auto-generated method stub
  JspFragment body=this.getJspBody();
  StringWriter sw=new StringWriter();
  if(body!=null){
   try {
    body.invoke(sw);
   } catch (JspException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return sw.toString();
 }
 
}

 

 

2.<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 version="2.0">
 <description>A tag library exercising SimpleTag handlers.</description>
 <tlib-version>1.0</tlib-version>
 <short-name>g</short-name>
 
 <uri>http://****.hbsi.3g/TagTest</uri>
 
 <tag>
  <name>TestIP</name>
  <tag-class>com.hbsi.****.web.TestIP</tag-class>
  <body-content>empty</body-content>
 </tag>
 
 <tag>
  <name>TagDemo1</name>
  <tag-class>com.hbsi.****.web.TagDemo1</tag-class>
  <body-content>JSP</body-content>
 </tag>
 <tag>
  <name>TagDemo2</name>
  <tag-class>com.hbsi.****.web.TagDemo2</tag-class>
  <body-content>empty</body-content>
 </tag>
 
 
 
 <tag>
  <name>LoopTag</name>
  <tag-class>com.hbsi.****.web.LoopTag</tag-class>
  <body-content>JSP</body-content>
  <attribute>
   <name>items</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
   <name>var</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 
  <tag>
  <name>ToUpperTag</name>
  <tag-class>com.hbsi.****.web.ToUpperTag</tag-class>
  <body-content>JSP</body-content>
  <attribute>
   <name>count</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 
 <tag>
  <name>SimpleTag</name>
  <tag-class>com.hbsi.****.web.SimpleTagTest</tag-class>
  <body-content>scriptless</body-content>
  <attribute>
   <name>counts</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 
 
 <tag>
  <name>to</name>
  <tag-class>com.hbsi.****.dd.to</tag-class>
  <body-content>JSP</body-content>
  <attribute>
   <name>count</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 
 
 <tag>
  <name>s</name>
  <tag-class>com.hbsi.****.dd.s</tag-class>
  <body-content>scriptless</body-content>
  <attribute>
   <name>counts</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 
 <tag>  
     <name>htmlfilter</name>  
     <tag-class>com.hbsi.****.dd.HtmlFilterTag</tag-class>  
     <body-content>JSP</body-content>  
</tag>  
<tag>  
     <name>readfile</name>  
     <tag-class>com.hbsi.****.dd.ReadFileTag</tag-class> 
          <body-content>empty</body-content>
     <attribute>
     <name>src</name>
     <required>true</required>
     </attribute> 
  
</tag> 

 
</taglib>
    3.<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://****.hbsi.3g/TagTest" prefix="tt"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'SimpleTagTest.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>
 
  This is my JSP page.
  <br>
  <tt:SimpleTag counts="3">
   aaaa
      </tt:SimpleTag>
 </body>
</html>