日期:2014-05-16 浏览次数:20342 次
package com.xll.util; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class MyTag extends TagSupport{ private String msg; public void setMsg(String msg){ this.msg = msg; } public MyTag(){ System.out.println("MyTag构造方法:一个myTag类的对象被构建了...."); } public int doStartTag() { JspWriter out = this.pageContext.getOut(); try { out.print(msg); } catch (IOException e) { e.printStackTrace(); } System.out.println("对象正在处理开始标记....."); return EVAL_BODY_INCLUDE; } public int doAfterBody() throws JspException{ System.out.println("处理标签体(after body)...."); return SKIP_BODY; } public int doEndTag() throws JspException{ System.out.println("对象正在处理结束标记....."); return EVAL_PAGE; } }
<?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.2</jsp-version> <short-name>my</short-name> <uri>xll</uri> <tag> <name>my</name> <tag-class>com.xll.util.MyTag</tag-class> <body-content>empty</body-content> <attribute> <name>msg</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
<%@ taglib prefix="my" uri="xll" %>
<body> <my:my msg="自定义标签"/> </body>