自定义标签的使用

1,写自定义标签类对象(MyTag.java)

package com.itheima.crm.utils;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 自定义标签的使用
 * @author Steven
 *
 */
public class MyTag extends TagSupport {

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = super.pageContext.getOut();
		
		try {
			out.print("<span>这个是自定义标签输出的内容<span>");
			out.print("<h1>这个是自定义标签输出的大标题<h1>");
			out.print("<p>这个是第一段</p>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();//获取上下文
	}

}

2,自定义标签描述文件(/WebContent/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>2.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>myTag</short-name>
	<uri>http://itheima/myTag/</uri>
	<tag>
		<name>out</name>
		<tag-class>com.itheima.crm.utils.MyTag</tag-class>
		<body-content>JSP</body-content>
	</tag>
</taglib>

3,自定义标签的使用

页面引入

<%@ taglib prefix="myTag" uri="http://itheima/myTag/"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="myTag" uri="http://itheima/myTag/"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>自定义标签的使用</title>
</head>
<body>
</body>
<myTag:out></myTag:out>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值