1.BodyTag.java
package com.zy.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class BodyTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
//获取标签体内容对象
JspFragment jb = this.getJspBody();
//获取PageContext对象
PageContext cOntext= (PageContext) this.getJspContext();
//获取JspWriter对象
JspWriter out = context.getOut();
//将标签体内容输出到指定writer对象中,循环5次
for (int i = 0; i <5; i++) {
jb.invoke(out);
}
out.flush();
super.doTag();
}
}
2.bodyTag.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.0tlib-version>
<jsp-version>1.0jsp-version>
<short-name>bodyTagshort-name>
<uri>http://www.zy.bodyTaguri>
<tag>
<name>bodyname>
<tag-class>com.zy.tag.BodyTagtag-class>
<body-content>scriptlessbody-content>
tag>
taglib>
3.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://www.zy.bodyTag" prefix="bodyTag" %>
DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首页title>
head>
<body background="../img/body-bg.png">
<hr/>
<bodyTag:body>
输出自定义标签体内容到页面demo!
bodyTag:body>
body>
html>
4.效果图