jsp自定义标签获取标签体内容输出到页面展示《三》
Posted anionic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp自定义标签获取标签体内容输出到页面展示《三》相关的知识,希望对你有一定的参考价值。
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自定义标签获取标签体内容输出到页面展示《三》的主要内容,如果未能解决你的问题,请参考以下文章