SimpleTagSupport自定义标签的使用

本文介绍如何使用SimpleTagSupport创建自定义JSP标签,包括创建继承类、配置tld文件及在JSP页面中的使用方法。并提供了多个示例,如输出文本、循环输出、修改输出内容等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用简单标签 很简单,分为以下步骤:

1. 创建一个类 继承SimpleTagSupport这个类  重写 他的doTag方法   其中 dotag方法中最重要的是 JspFragment jf = this.getJspBody();  这个获取标签体  然后进行一系列操作

2. 创建一个tld文件  里面配置 标签信息 :

   

<?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>ys</short-name>
    <uri>http://yangsheng.cn</uri>
    <tag>
        <name>ViewIp</name>
	<tag-class>cn.web.tag.Tag1</tag-class>
	<body-content>empty</body-content>
    </tag>
     <tag>
        <name>demo1</name>
	<tag-class>simpleTag.Demo1</tag-class>
	<body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>demo2</name>
	<tag-class>simpleTag.Demo2</tag-class>
	<body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>demo3</name>
	<tag-class>simpleTag.Demo3</tag-class>
	<body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>demo4</name>
	<tag-class>simpleTag.Demo4</tag-class>
	<body-content>empty</body-content>
    </tag>
    <tag>
        <name>demo5</name>
	<tag-class>simpleTag.Demo5</tag-class>
	<body-content>scriptless</body-content>
	<attribute>
		<name>count</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	
	<attribute>
		<name>date</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	    
	</attribute>
	
    </tag>
    </taglib>
    
类似于这个样子的 

3.  在jsp中使用标签:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://yangsheng.cn" prefix="ys" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%--<ys:demo4/>    --%>
<html>
  <head>

  </head>
  
  <body>
  输出内容: <ys:demo1>
  			aaaaa
       </ys:demo1>
<hr>
循环输出:    <ys:demo2>
			bbbbb
		</ys:demo2>
  <hr>
  修改输出:<ys:demo3>
  		ccccc
  </ys:demo3>
  <hr>
  带参数的标签:<ys:demo5 count="10" date="<%=new Date() %>">
  dddd
  </ys:demo5>
  
  </body>
</html>

1.  使用标签实现输出文本内容 

 

JspFragment jf = this.getJspBody();
		jf.invoke(null); //默认输出标签体
2.循环输出

JspFragment jf = this.getJspBody();
		for (int i = 0; i < 4; i++) {
			jf.invoke(null);
		}

3.修改输出

JspFragment jf = this.getJspBody();
		StringWriter sw = new StringWriter();
		jf.invoke(sw);
		String content = sw.toString();
		
		this.getJspContext().getOut().print(content+"aaaaa");

4.控制下面内容不输出

throw new SkipPageException();


5.带参数的 标签
private int count;
	private Date date;

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


	public void setDate(Date date) {
		this.date = date;
	}


	@Override
	public void doTag() throws JspException, IOException {
		JspFragment jf = this.getJspBody();
		this.getJspContext().getOut().write(date.toLocaleString());
		for(int i = 0;i<count;i++){
			jf.invoke(null);
		}
		
	}
	

 <tag>
        <name>demo5</name>
	<tag-class>simpleTag.Demo5</tag-class>
	<body-content>scriptless</body-content>
	<attribute>
		<name>count</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	
	<attribute>
		<name>date</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	    
	</attribute>
	
    </tag>

name--标签名    required--属性是否必须  rtexprvalue--是否可用EL表达式 和<%%>代码块  type--属性的类型(可不写) 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值