您的位置:首页 > 其它

【自定义标签开发】02-自定义标签运行原理

2016-04-06 09:02 225 查看
我们说一下自定义标签的原理。

我们上一次在页面中引入了我们自己开发的自定义标签<zyg:viewIP>,那么这个标签是如何去调用我们的自定义标签的java类的呢?

我们通过标签调用图来理解一下原理:



问题:导入自定义标签,不用指定是命名空间下的哪个tld文件吗?

答:不用,Web服务器会自动搜索。Web应用在服务器中加载的时候,服务器会搜索WEB-INF下的所有的tld文件,全部加载到内存中,然后加载到一个Map集合中去,key就是你在tld中定义的uri。

我们还可以看一个东西,来证明我们的标签是调用了标签处理器类。我们都知道jsp在服务器中会被翻译成Servlet,那么我们去服务器中去查看被翻译的Servlet,看一下我们之前的<viewIP/>被翻译成了什么:

我们在tomcat中找到index被翻译的servlet:

apache-tomcat-6.0.24\work\Catalina\localhost\JspTest\org\apache\jsp\index_jsp.java:
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {

private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

private static java.util.List _jspx_dependants;

static {
_jspx_dependants = new java.util.ArrayList(1);
_jspx_dependants.add("/WEB-INF/lib/zyg.tld");
}

private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody;

private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;

public Object getDependants() {
return _jspx_dependants;
}

public void _jspInit() {
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}

public void _jspDestroy() {
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.release();
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;

try {
response.setContentType("text/html;charset=utf-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write("  <head>\r\n");
out.write("    <title>My JSP 'index.jsp' starting page</title>\r\n");
out.write("  </head>\r\n");
out.write("  \r\n");
out.write("  <body>\r\n");
out.write("    您的ip为:\r\n");
out.write("    ");
if (_jspx_meth_zyg_005fviewIP_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write("  </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}

private boolean _jspx_meth_zyg_005fviewIP_005f0(PageContext _jspx_page_context)
throws Throwable {
PageContext pageContext = _jspx_page_context;
JspWriter out = _jspx_page_context.getOut();
//  zyg:viewIP
org.zyg.web.tag.ViewIPTag _jspx_th_zyg_005fviewIP_005f0 = (org.zyg.web.tag.ViewIPTag) _005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.get(org.zyg.web.tag.ViewIPTag.class);
_jspx_th_zyg_005fviewIP_005f0.setPageContext(_jspx_page_context);
_jspx_th_zyg_005fviewIP_005f0.setParent(null);
int _jspx_eval_zyg_005fviewIP_005f0 = _jspx_th_zyg_005fviewIP_005f0.doStartTag();
if (_jspx_th_zyg_005fviewIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.reuse(_jspx_th_zyg_005fviewIP_005f0);
return true;
}
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.reuse(_jspx_th_zyg_005fviewIP_005f0);
return false;
}
}


我们可以看到这一段:
out.write("    您的ip为:\r\n");
out.write("    ");
if (_jspx_meth_zyg_005fviewIP_005f0(_jspx_page_context))
return;
out.write("\r\n");


我们在您的ip为:后加入的<viewIP/>标签,所以下面在if中调用了_jspx_meth_zyg_005fviewIP_005f0()方法,并将_jspx_page_context作为参数传入进去了:
private boolean _jspx_meth_zyg_005fviewIP_005f0(PageContext _jspx_page_context)
throws Throwable {
PageContext pageContext = _jspx_page_context;
JspWriter out = _jspx_page_context.getOut();
//  zyg:viewIP
org.zyg.web.tag.ViewIPTag _jspx_th_zyg_005fviewIP_005f0 = (org.zyg.web.tag.ViewIPTag) _005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.get(org.zyg.web.tag.ViewIPTag.class);
_jspx_th_zyg_005fviewIP_005f0.setPageContext(_jspx_page_context);
_jspx_th_zyg_005fviewIP_005f0.setParent(null);
int _jspx_eval_zyg_005fviewIP_005f0 = _jspx_th_zyg_005fviewIP_005f0.doStartTag();
if (_jspx_th_zyg_005fviewIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.reuse(_jspx_th_zyg_005fviewIP_005f0);
return true;
}
_005fjspx_005ftagPool_005fzyg_005fviewIP_005fnobody.reuse(_jspx_th_zyg_005fviewIP_005f0);
return false;
}


通过这个方法我们可以看到,方法运行的代码顺序为:

(1)拿PageContext和JspWriter

PageContext pageContext = _jspx_page_context;

JspWriter out = _jspx_page_context.getOut();

(2)创建标签处理器类对象:

get(org.zyg.web.tag.ViewIPTag.class);

(3)设置PageContext:

setPageContext(_jspx_page_context);

(4)设置Parent:

setParent(null);

(5)执行doStartTag方法:

doStartTag();

(6)执行doEndTag方法:

(7)doEndTag();

将标签放入缓存中以供下次调用:

(8)reuse();

以上就是自定义标签的执行原理。大家可以自己去查看相应的源码来剖析。

转载请注明出处:http://blog.csdn.net/acmman/article/details/51071894
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: