1.开发第一个自定义标签

=====================================================
************************************************************************************
=====================================================


------------------------------------具体实现-----------------------------------------------------

java类:

前台为自定义标签,必须写tld文件:

tld文件所在位置:

Tomcat服务器的tld文件的模板存放位置:


<description>描述
<name>viewIp</name>JSP页面上写的标签名
<tag-class>标签类的完整路径,例如:包名.类名
<body-content>empy</body-content> 没有标签体,是自闭的标签:<veweIP/>
-----------------将自定义的标签,绑定到一个<uri>上,方便JSP页面引用标签----------------


自定义标签 生成的JSP源码:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.77
* Generated at: 2018-04-28 15:37:06 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent
{
private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
static
{
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
_jspx_dependants.put("/WEB-INF/mytag.tld", Long.valueOf(1524929755928L));
}
private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fmytag_005fMyTag_005fnobody;
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants()
{
return _jspx_dependants;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory()
{
if (_el_expressionfactory == null)
{
synchronized (this)
{
if (_el_expressionfactory == null)
{
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager()
{
if (_jsp_instancemanager == null)
{
synchronized (this)
{
if (_jsp_instancemanager == null)
{
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit()
{
_005fjspx_005ftagPool_005fmytag_005fMyTag_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
}
public void _jspDestroy()
{
_005fjspx_005ftagPool_005fmytag_005fMyTag_005fnobody.release();
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
{
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.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("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<h1>Hello jsp</h1>\r\n");
out.write("\tIPΪ:");
if (_jspx_meth_mytag_005fMyTag_005f0(_jspx_page_context))
return;
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t)
{
if (!(t instanceof javax.servlet.jsp.SkipPageException))
{
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted())
{
out.flush();
} else
{
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally
{
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
//自定义标签
private boolean _jspx_meth_mytag_005fMyTag_005f0(javax.servlet.jsp.PageContext _jspx_page_context) throws java.lang.Throwable
{
javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
// mytag:MyTag
mytag.MyTag _jspx_th_mytag_005fMyTag_005f0 = (mytag.MyTag) _005fjspx_005ftagPool_005fmytag_005fMyTag_005fnobody.get(mytag.MyTag.class);
boolean _jspx_th_mytag_005fMyTag_005f0_reused = false;
try {
_jspx_th_mytag_005fMyTag_005f0.setPageContext(_jspx_page_context);//执行自动以标签的setPageContext( PageContext pc )方法
_jspx_th_mytag_005fMyTag_005f0.setParent(null);//调用自定义标签的setParent(Tag t)方法。 自定义标签初始化工作到此完成
int _jspx_eval_mytag_005fMyTag_005f0 = _jspx_th_mytag_005fMyTag_005f0.doStartTag();//执行doStartTag()方法
if (_jspx_th_mytag_005fMyTag_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) //执行doEndTag()方法
{
return true;
}
_005fjspx_005ftagPool_005fmytag_005fMyTag_005fnobody.reuse(_jspx_th_mytag_005fMyTag_005f0);
_jspx_th_mytag_005fMyTag_005f0_reused = true;
} finally
{
org.apache.jasper.runtime.JspRuntimeLibrary.releaseTag(_jspx_th_mytag_005fMyTag_005f0, _jsp_getInstanceManager(), _jspx_th_mytag_005fMyTag_005f0_reused);
}
return false;
}
}
浙公网安备 33010602011771号