自定义JSP标记和Tomcat 8

时间:2015-04-27 13:09:31

标签: java jsp tomcat8

我对自定义JSP标记和Tomcat 8有一些奇怪的错误。使用Tomcat 7它可以工作。

TLD文件:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib version="2.0"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
   <tlib-version>1.0</tlib-version>
   <short-name>Example TLD</short-name>
   <uri>http://www.hello.com/tags/hello</uri>
   <tag>
      <name>Hello</name>
      <tag-class>myapp.tag.HelloTag</tag-class>
      <body-content>empty</body-content>
   </tag>
</taglib>

处理程序类:

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        out.println("Hello Custom Tag!");
    }
}

JSP(仅限第6行):

...
<%@ taglib prefix="ex" uri="/WEB-INF/lib/custom.tld"%>
...

错误:

Caused by: org.apache.jasper.JasperException: /WEB-INF/jsp/myapp.jsp (line: 6, column: 1) null
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:275)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:91)
at org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:325)
at org.apache.jasper.compiler.Parser.parseIncludeDirective(Parser.java:358)
at org.apache.jasper.compiler.Parser.parseXMLDirective(Parser.java:527)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1432)
at org.apache.jasper.compiler.Parser.parse(Parser.java:139)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:570)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:523)
at org.apache.tiles.request.servlet.ServletRequest.doInclude(ServletRequest.java:243)

2 个答案:

答案 0 :(得分:0)

我注意到了一些事情:

  1. 尝试在uri中包含一个开头/符号,如下所示:<%@ taglib prefix="ex" uri="/WEB-INF/lib/custom.tld" %>
  2. 您在问题中提供的TLD文件(希望名为custom.tld)看起来不完整。它错过了结束标记。它缺少初始定义。
  3. 我的TLD文件如下所示:

    <?xml version="1.0"?>
    <taglib version="2.0"
            xmlns="http://java.sun.com/xml/ns/j2ee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
      <tlib-version>1.0</tlib-version>
      <short-name>custom</short-name>
      <uri>http://example.com/tags/custom</uri>
      <tag>
        ...
      </tag>
    </taglib>
    

    在JSP中我通过这样的URI导入它们,它在Tomcat 8上运行正常:

    <%@ taglib prefix="cust" uri="http://example.com/tags/custom" %>
    

答案 1 :(得分:0)

Tomcat 8期望WEB-INF文件夹中的tld文件。 将custom.tld文件移动到那里并将jsp文件中的第6行更正为:

<%@ taglib prefix="ex" uri="/WEB-INF/custom.tld"%>

现在它应该工作