简体   繁体   中英

Custom JSP Tag and Tomcat 8

I have strange error with custom JSP tag and Tomcat 8. With Tomcat 7 it works.

The TLD-File:

<?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>

Handler Class:

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 (only line 6):

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

And the error:

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)

I noticed a few things:

  1. Try to include a beginning / symbol in the uri like this: <%@ taglib prefix="ex" uri="/WEB-INF/lib/custom.tld" %>
  2. The TLD file (hopefully named custom.tld) that you gave in your question looks incomplete. It's missing the end tag. It's missing the initial definition.

My TLD files look something like this:

<?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>

And in the JSP I import them via URI like this, and it works fine on Tomcat 8:

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

Tomcat 8 expects the tld file in the WEB-INF Folder. Move the custom.tld file there and correct line 6 in the jsp File to:

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

Now it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM