将自定义标签库添加到 JSP 时出现异常

Exception when adding custom tag library to JSP

我和 this unanswered question 差不多 2 年前有过同样的问题。

<%@ taglib uri="/WEB-INF/tlds/myLib" prefix="myLib" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

我的 JSP 中有这两行。最初我只使用 JSTL 核心库并且一切正常。一旦添加自定义库,就会出现这样的异常:

java.lang.NullPointerException
at org.apache.jasper.compiler.TagLibraryInfoImpl.toString(TagLibraryInfoImpl.java:129)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.concurrent.ConcurrentHashMap.toString(ConcurrentHashMap.java:1321)
at org.apache.taglibs.standard.tag.common.core.OutSupport.out(OutSupport.java:211)
at org.apache.taglibs.standard.tag.common.core.OutSupport.doStartTag(OutSupport.java:132)
at org.apache.jsp.FormTest_jsp._jspx_meth_c_out_3(FormTest_jsp.java:306)
at org.apache.jsp.FormTest_jsp._jspx_meth_c_forEach_1(FormTest_jsp.java:256)
at org.apache.jsp.FormTest_jsp._jspService(FormTest_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
at form.FormServlet.processRequest(FormServlet.java:60)
at form.FormServlet.doPost(FormServlet.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

如果我删除两个 taglib 指令中的任何一个,一切都会正常工作。

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>mylib</short-name>
  <uri>/WEB-INF/tlds/myLib</uri>
  <tag>
    <name>beaner</name>
    <tagclass>form.BeanTagHandler</tagclass>
    <bodycontent>empty</bodycontent>
  </tag>
</taglib>

tld 是非常小的尝试让它工作。

如果有人能提供一些关于问题可能是什么的见解,我们将不胜感激。谢谢。

编辑:找到问题的根源。

<table>
<tr>
<th>Attribute Name</th>
<th>Attribute Value</th></tr>
<c:forEach var="a" items="${applicationScope}">
<tr>
<td> <c:out value="${a.key}"/> </td>
<td> <c:out value="${a.value}" /> </td>
</tr>
</c:forEach>
</table>

有问题的部分是 ${a.value} 的 c:out。 jsp 没有它也能正常工作。 出于某种原因,当我不导入我的自定义标签库时它工作正常。关于为什么尝试 c:out applicationScope 的值会导致问题的任何想法?

好的,我发现问题了。 applicationScope 中有一个名为com.sun.jsp.taglibraryCache 的属性,即Class ConcurrentHashMap。似乎导入另一个标签库导致 class 的 toString 方法给出 NullPointerException。我通过使用 c:catch 来捕获异常来解决我的问题。