自定义标签执行顺序实验:
doStartTag()中的
return EVAL_BODY_INCLUDE
顺序:
setPageContext()
SetParent()
doStartTag()
doAfterBody()
doEndTag()
return SKIP_BODY
setPageContext()
setParent()
doStartTag()
doEndTag()
return EVAL_PAGE
执行顺序:
setPageContext()
setParent()
doStartTag()
setBodyContent()
doinitBody()
doAfterBody()
doEndTag()
release()
package com.chen.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;
public class HelloTag implements BodyTag {
private PageContext pageContext;
private Tag parent;
public int doAfterBody() throws JspException {
System.out.println("doAfterBody()");
return 0;
}
public int doEndTag() throws JspException {
System.out.println("doEndTag()");
try {
pageContext.getOut().println("hello world!");
} catch (Exception e) {
// TODO: handle exception
}
return 0;
}
public int doStartTag() throws JspException {
System.out.println("doStartTag()");
//EVAL_BODY_INCLUDE 表示显示标签之间的内容
//<mytag:hello>God</mytag:hello>
//Godhello world!
//SKIP_BODY的话,则不显示了
//return EVAL_PAGE
return SKIP_BODY;
}
public Tag getParent() {
System.out.println("getParent()");
return null;
}
public void release() {
System.out.println("release()");
}
public void setPageContext(PageContext pageContext) {
System.out.println("setPageContext()");
this.pageContext=pageContext;
}
public void setParent(Tag tag) {
System.out.println("SetParent()");
this.parent=tag;
}
public void doInitBody() throws JspException {
System.out.println("doinitBody()");
}
public void setBodyContent(BodyContent arg0) {
System.out.println("setBodyContent()");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/hellotag.tld" prefix="mytag" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<mytag:hello>dongdong</mytag:hello>
</body>
</html>
WEB-INF/hellotag.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.2</jsp-version>
<short-name>tagSample</short-name>
<uri>/hellotag</uri>
<tag>
<name>hello</name>
<tag-class>com.chen.tag.HelloTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
后续:
比如使用:
<mytag:hello id="hello" name="mygod">dongdong</mytag:hello>
那么执行顺序会是:
setPageContext()
SetParent()
setID()
setName()
...[如果还有其他属性设置的话]
doStartTag()
381

被折叠的 条评论
为什么被折叠?



