NPE参数错误:仅当<h:head>在页面</h:head>时,参数值为空(JSF)

时间:2011-12-08 15:37:08

标签: jsf jar

我有一个jsf页面,main.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">

<ui:composition template="WEB-INF/templates/layout.xhtml">
    <ui:define name="title">Page Title</ui:define>
...

Layout.xml如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">
<h:head>
    <title><ui:insert name="title">Default Title</ui:insert></title>
    <h:outputStylesheet library="theme" name="main.css" />
</h:head>

<h:body>

<div id="header">
    <ui:insert name="header">
        <ui:include src="header.xhtml"/>
    </ui:insert>
    <ui:insert name="toolbar">
         <ui:include src="toolbar.xhtml" />
    </ui:insert>
</div>


<div id="content">
    <br/>
    <ui:insert name="content">
        TBD
    </ui:insert>
</div>

<div id="footer">
    <ui:insert name="footer">
        <ui:include src="footer.xhtml"/>  
    </ui:insert>
</div>

</h:body>

</html>

当我尝试加载页面时,我得到以下堆栈跟踪:

Dec 8, 2011 8:58:50 AM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/main.xhtml]
java.lang.NullPointerException: Argument Error: Parameter value is null
    at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.writeURIAttribute(HtmlResponseWriter.java:985)
    at com.sun.faces.renderkit.html_basic.StylesheetRenderer.encodeEnd(StylesheetRenderer.java:108)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at com.sun.faces.renderkit.html_basic.HeadRenderer.encodeHeadResources(HeadRenderer.java:105)
    at com.sun.faces.renderkit.html_basic.HeadRenderer.encodeEnd(HeadRenderer.java:92)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.lmig.cm.ps.editor.filter.SecuredPageFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.lmig.cm.ps.editor.user.UserFilter.handleDeveloperEnvironment(Unknown Source)
    at com.lmig.cm.ps.editor.user.UserFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.lmig.cm.ps.editor.filter.SessionTimeoutFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

如果我从layout.xhtml中删除该部分:

<h:head>
    <title><ui:insert name="title">Default Title</ui:insert></title>
    <h:outputStylesheet library="theme" name="main.css" />
</h:head>

页面将加载(当然没有css或标题)。即使我在页面中只有这个:

<h:head>
</h:head>

它不会渲染,我收到同样的错误。

我已经工作了很长一段时间,无法弄清楚我做错了什么。我的预感是,它与我所包含的罐子的版本有关,但是将大部分罐子换掉,都没有找到解决方案。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:2)

java.lang.NullPointerException: Argument Error: Parameter value is null 
    at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.writeURIAttribute(HtmlResponseWriter.java:985)
    at com.sun.faces.renderkit.html_basic.StylesheetRenderer.encodeEnd(StylesheetRenderer.java:108)

让我们看一下StylesheetRenderer#encodeEnd()来源(我正在使用Mojarra 2.1.4;我假设您也使用它,因为大多数(如果不是所有com.sun.faces堆栈跟踪中的行号都匹配100 %来源;未来请提及确切版本):

105    String resourceUrl = "RES_NOT_FOUND";
105    if (resource != null) {
106        resourceUrl = context.getExternalContext().encodeResourceURL(resource.getRequestPath());
107    }
108    writer.writeURIAttribute("href", resourceUrl, "href");

所以,resourceUrl不知何故是null

让我们看一下ExternalContext#encodeResourceURL(String)来源:

311     public abstract String encodeResourceURL(String url);

噢,这是抽象的。好吧,让我们看看默认的Mojarra实现,com.sun.faces.context.ExternalContextImpl

521     public String encodeResourceURL(String url) {
522         return ((HttpServletResponse) response).encodeURL(url);
523     }

啊,它委托给HttpServletResponse#encodeURL(),默认情况下不应该返回null

因此,您的问题可能至少有两个可能的原因:

  • 您的自定义ExternalContext实施工具无法正常完成encodeResourceURL()工作。
  • 您的自定义HttpServletResponseWrapper实施工具无法正常完成encodeURL()工作。

将调试器断点放在StylesheetRenderer的第106行并从那里进行调试 应该告诉你实际使用了哪些实现。

当你删除<h:outputStylesheet>时发生同样的异常可能是因为RichFaces隐式地在那里插入那些组件以及自己的样式表。根据您的XML名称空间声明,您使用的是RichFaces。