1. 程式人生 > >jsp自定義標簽獲取標簽體內容輸出到頁面展示《三》

jsp自定義標簽獲取標簽體內容輸出到頁面展示《三》

ride write bubuko his color back lan invoke mage

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.0</tlib-version> <jsp-version>1.0</jsp-version> <short-name>bodyTag</short-name> <uri>http://www.zy.bodyTag</uri> <tag> <name>body</name> <tag-class>com.zy.tag.BodyTag</tag-class> <body-content>scriptless</body-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.效果圖

技術分享圖片

jsp自定義標簽獲取標簽體內容輸出到頁面展示《三》