簡體   English   中英

在JSP中添加自定義taglib時出現NullPointerException

[英]NullPointerException when add custom taglib in JSP

我需要為JSP頁面實現一些自定義函數,為此我需要創建自定義taglib。 這應該是簡單的操作,但是當我添加空的taglib時,我在第一步遇到了問題。 我有一個例外:

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)
    ...

我的自定義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:

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

另外,我嘗試在custom.tld中添加一些函數,但沒有任何更改。

有人能幫我嗎? 我究竟做錯了什么?

您需要將custom.tld文件移動到/WEB-INF目錄中的某個位置,以便容器找到並將其映射到/<taglib>/<uri>提供的值。

如果由於某種原因你不能,你應該在你的web.xml文件中添加一個<taglib>映射。

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

<taglib-location>是相對於Web應用程序根目錄指定的。 如果需要,修改。

我花了4個小時來解決這個問題。 我已經從Tomcat 6切換到Tomcat 8並開始獲得相同的NPE。 我最終發現問題在於導入我的taglib

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

事實證明,Tomcat 8 Jasper在相對路徑上的導入失敗。 我改成了

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

並在我的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