自定义标签

本文介绍了如何在JSP中自定义标签,包括简单的标签定义和带有属性的标签定义过程。通过实例展示了如何创建标签类、配置tld文件以及在JSP页面中使用这些自定义标签。

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

一、自定义一个标签

 

1、定义Person类,他继承SimpleTagSupport

package com.sunrex.demo07;

import java.io.IOException;

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

public class Person extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		getJspContext().getOut().write("hello, world!11!");
	}
}

 

2、定义WEB-INF/person.tld文件

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

<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.3</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>person</short-name>
	<uri>http://www.sunrex.com/person</uri>
	<tag>
		 <!-- 定义标签名 -->
        <name>name</name>
        <!-- 定义标签处理类 -->
        <tag-class>com.sunrex.demo07.Person</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>empty</body-content>
	</tag>
</taglib>

3、在jsp页面中使用标签

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!-- 引入标签 -->    
<%@ taglib prefix="person" uri="http://www.sunrex.com/person" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<!-- 使用标签 -->
	<person:name/>
</body>
</html>

  

4、输出的结果是:hello, world!11!

 

二、定义带属性的标签

1、定义Person类

package com.sunrex.demo07;

import java.io.IOException;

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

public class Person extends SimpleTagSupport {
	private String username;
	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public void doTag() throws JspException, IOException {
		getJspContext().getOut().write("<br>属性username的值为=" + this.getUsername());
		if(this.password == null || this.password.length()<1) {
			getJspContext().getOut().write("<br>属性password没有值");
		} else {
			getJspContext().getOut().write("<br>password属性的值为:" + this.getPassword());
		}
	}
}

 2、定义tld标签

 

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

<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.3</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>person</short-name>
	<uri>http://www.sunrex.com/person</uri>
	<tag>
		 <!-- 定义标签名 -->
        <name>name</name>
        <!-- 定义标签处理类 -->
        <tag-class>com.sunrex.demo07.Person</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>empty</body-content>
        
        <attribute><!-- 定义标签属性 -->
        	<name>username</name>
        	<required>true</required><!-- 必须属性 -->
        </attribute>
         <attribute>
        	<name>password</name>
        	<required>false</required><!-- 不是必须属性 -->
        </attribute>
	</tag>
</taglib>

 

3、在jsp页面的调用

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!-- 引入标签 -->    
<%@ taglib prefix="person" uri="http://www.sunrex.com/person" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<!-- 使用标签 -->
	<person:name username="jhlishero"/>
	<person:name username="东南西北风" password="jhl"/>
</body>
</html>

 三、定义循环输出的标签:如iterator

待完善

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值