简体   繁体   English

在JSP中添加自定义taglib时出现NullPointerException

[英]NullPointerException when add custom taglib in JSP

I need to implement a few custom functions for JSP page and for this I need to create custom taglib. 我需要为JSP页面实现一些自定义函数,为此我需要创建自定义taglib。 It should be simple action, but I got a problem on first step when I just add empty taglib. 这应该是简单的操作,但是当我添加空的taglib时,我在第一步遇到了问题。 I got a exception: 我有一个例外:

java.lang.NullPointerException
    at org.apache.tomcat.util.descriptor.tld.TldResourcePath.hashCode(TldResourcePath.java:156)
    at java.util.HashMap.hash(HashMap.java:338)
    at java.util.HashMap.get(HashMap.java:556)
    at org.apache.jasper.compiler.TldCache.getTaglibXml(TldCache.java:95)
    at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:179)
    at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:411)
    at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:469)
    at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1428)
    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)
    ...

My custom tld(placed in webapp/custom.tld): 我的自定义tld(放在webapp / custom.tld中):

<?xml version="1.0">
<taglib 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/web-jsptaglibrary_2_0.xsd"
        version="2.0">

    <tlib-version>1.0</tlib-version>
    <short-name>MyLibrary</short-name>
    <uri>myTagLib</uri>

</taglib>

JSP: JSP:

<%@ taglib prefix="myTag" uri="myTagLib" %>

Also, I tried to add some function in custom.tld, but without any changes. 另外,我尝试在custom.tld中添加一些函数,但没有任何更改。

Can someone help me? 有人能帮我吗? What am i doing wrong? 我究竟做错了什么?

You need to move your custom.tld file somewhere inside the /WEB-INF directory for the container to find and map it to the value provided at /<taglib>/<uri> . 您需要将custom.tld文件移动到/WEB-INF目录中的某个位置,以便容器找到并将其映射到/<taglib>/<uri>提供的值。

If for some reason you can't, you should add a <taglib> mapping to your web.xml file. 如果由于某种原因你不能,你应该在你的web.xml文件中添加一个<taglib>映射。

<jsp-config>
  <taglib>
    <taglib-uri>myTagLib<taglib-uri>
    <taglib-location>/webportal/custom.tld<taglib-location>
  </taglib>
</jsp-config>

The <taglib-location> is specified relative to your web application root. <taglib-location>是相对于Web应用程序根目录指定的。 Modify, if required. 如果需要,修改。

I've spent 4 hours on fixing this. 我花了4个小时来解决这个问题。 I've switched from Tomcat 6 to Tomcat 8 and started getting same NPE. 我已经从Tomcat 6切换到Tomcat 8并开始获得相同的NPE。 I ended up finding that the problem was in importing my taglib 我最终发现问题在于导入我的taglib

<%@ taglib uri="/includes/tt.tld" prefix="tt" %>

Turned out that Tomcat 8 Jasper fails on imports with relative path. 事实证明,Tomcat 8 Jasper在相对路径上的导入失败。 I changed it to 我改成了

<%@ taglib uri="myTagLib" prefix="tt" %>

and defined taglib in my web.xml as 并在我的web.xml定义了taglib

<jsp-config>
    <taglib>
        <taglib-uri>myTagLib</taglib-uri>
        <taglib-location>/includes/tt.tld</taglib-location>
    </taglib>
</jsp-config>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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