日期:2014-05-16 浏览次数:20546 次
Jsp2.0后,实现tag的方式除了taglib(TLD)的方式外,还可以通过定义tag文件来代替taglib类。tag file一般放在/WEB-INF/tags目录或者其子目录,需要在jsp文件中指定uri。
例子来源
注:jsp和tag file在Tomcat7下编译。
firstTagTest.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %> Today is <easy:firstTag/> //包含tag,没有参数传递
firstTag.tag
//直接被firstTagTest.jsp引入编译为java/class文件 <%@ tag import="java.util.Date" import="java.text.DateFormat"%> <% DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); Date now = new Date(System.currentTimeMillis()); out.println(dateFormat.format(now)); %>
firstTag_tag.java
public void doTag() throws javax.servlet.jsp.JspException, java.io.IOException { javax.servlet.jsp.PageContext _jspx_page_context = (javax.servlet.jsp.PageContext)jspContext; javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest) _jspx_page_context.getRequest(); javax.servlet.http.HttpServletResponse response = (javax.servlet.http.HttpServletResponse) _jspx_page_context.getResponse(); javax.servlet.http.HttpSession session = _jspx_page_context.getSession(); javax.servlet.ServletContext application = _jspx_page_context.getServletContext(); javax.servlet.ServletConfig config = _jspx_page_context.getServletConfig(); javax.servlet.jsp.JspWriter out = jspContext.getOut(); _jspInit(config); jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,jspContext); try { out.write('\r'); out.write('\n'); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); Date now = new Date(System.currentTimeMillis()); out.println(dateFormat.format(now)); out.write('\r'); out.write('\n'); } catch( java.lang.Throwable t ) { if( t instanceof javax.servlet.jsp.SkipPageException ) throw (javax.servlet.jsp.SkipPageException) t; if( t instanceof java.io.IOException ) throw (java.io.IOException) t; if( t instanceof java.lang.IllegalStateException ) throw (java.lang.IllegalStateException) t; if( t instanceof javax.servlet.jsp.JspException ) throw (javax.servlet.jsp.JspException) t; throw new javax.servlet.jsp.JspException(t); } finally { jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,super.getJspContext()); ((org.apache.jasper.runtime.JspContextWrapper) jspContext).syncEndTagFile(); } }
firstTagTest_jsp.java
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.