当前位置:编程学习 > JS >>

Jsp tag file例子以及说明

简介
Jsp2.0后,实现tag的方式除了taglib(TLD)的方式外,还可以通过定义tag文件来代替taglib类。tag file一般放在/WEB-INF/tags目录或者其子目录,需要在jsp文件中指定uri。

例子1 将tag file作为内容直接引入
       firstTagTest.jsp

[java] 
<%@ page contentType="text/html; charset=UTF-8" %> 
<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %> 
Today is <easy:firstTag/> //包含tag,没有参数传递 


firstTag.tag

[java] 
//直接被firstTagTest.jsp引入编译为java/class文件 
<%@ tag import="java.util.Date" import="java.text.DateFormat"%> 
<%  一时手易做图,把structs-config.xml文件中的一段话删了,结果老是报错。
报错内容:Document is invalid: no grammar found
[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?> 
<strong><span style="color:#ff0000;"><!DOCTYPE struts-config PUBLIC 
       "-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN" 
       "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"></span></strong> 
<struts-config> 
    <form-beans> 
        <form-bean name="helloForm" type="com.mars.demo01.structs01.form.HelloForm" /> 
    </form-beans> 
    <!-- 用于配置ActionForm --> 
    <global-exceptions /><!-- 用于配置全局异常 --> 
    <global-forwards /><!-- 用于配置全局跳转 --> 
    <action-mappings> 
        <action attribute="helloForm" input="/demo01/demo01_hello.jsp" 
            name="helloForm" path="/demo01/demo01_hello" scope="request" 
            type="com.mars.demo01.structs01.action.HelloAction" cancellable="true"> 
            <forward name="show" path="/demo01/demo01_hello.jsp"></forward> 
        </action> 
 
    </action-mappings> 
    <!-- 用于配置Action --> 
    <message-resources parameter="com.mars.demo01.structs01.ApplicationResources" /> 
</struts-config> 
然后恢复原样,结果又好了,所以这句话是不能删的。在网上查了查,发现这是一个定义格式的东东。怎么弄的这么麻烦。

  DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); 
  Date now = new Date(System.currentTimeMillis()); 
  out.println(dateFormat.format(now)); 
%> 

     编译后的代码
     firstTag_tag.java

    

[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


[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.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(); 
   

补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,