FacesContext.getCurrentInstance()在FacesServlet(?)处理的请求中返回null

时间:2012-02-03 22:01:22

标签: java jsf java-ee jsf-2

我有一个有两个<h:forms>的Facelet。一个表单中的命令链接执行没有问题。另一种形式的命令链接不起作用:抛出NullPointerException因为FacesContext.getCurrentInstance()正在返回null。在我看来,请求通过FacesServlet,因此FacesContext.getCurrentInstance()返回null没有意义。

这是XHTML:

<h:form>
    <h:commandLink action="#{localeManager.setLanguage('en')}">English</h:commandLink>
</h:form>

生成的HTML在这里:

<form id="j_idt20" name="j_idt20" method="post" action="/Patrac/faces/academicyear.xhtml" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="j_idt20" value="j_idt20" />
    <script type="text/javascript" src="/Patrac/faces/javax.faces.resource/jsf.js?ln=javax.faces&amp;stage=Development"></script>
    <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt20'),{'j_idt20:j_idt22':'j_idt20:j_idt22'},'');return false">English</a>
    <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-3103232499387158697:-8681635922139458233" autocomplete="off" />
</form>

这是LocaleManager:

@Named
@SessionScoped
public class LocaleManager implements Serializable
{
...
    public void setLanguage(String language)
    {
        this.locale = new Locale(language);
        FacesContext fc = FacesContext.getCurrentInstance();


        UIViewRoot uivr = fc.getViewRoot(); <===== NullPointerException

        uivr.setLocale(locale);
    }
}

这是堆栈跟踪:

16:35:31,697 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http--127.0.0.1-8443-4) #{localeManager.setLanguage('en')}: java.lang.NullPointerException: javax.faces.FacesException: #{localeManager.setLanguage('en')}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.7.Final.jar:]
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.4.Final.jar:2011-11-22 20:01]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.7.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.0.CR1b.jar:7.1.0.CR1b]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:151) [jboss-as-web-7.1.0.CR1b.jar:7.1.0.CR1b]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.7.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.7.Final.jar:]
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897) [jbossweb-7.0.7.Final.jar:]
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.7.Final.jar:]
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2033) [jbossweb-7.0.7.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [:1.7.0_02]
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    ... 24 more
Caused by: java.lang.NullPointerException
    at com.patrac.controller.LocaleManager.setLanguage(LocaleManager.java:38) [Patrac-ejb-1.0-SNAPSHOT.jar:]
    at com.patrac.controller.LocaleManager$Proxy$_$$_WeldClientProxy.setLanguage(LocaleManager$Proxy$_$$_WeldClientProxy.java) [Patrac-ejb-1.0-SNAPSHOT.jar:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.7.0_02]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.7.0_02]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.7.0_02]
    at java.lang.reflect.Method.invoke(Method.java:601) [:1.7.0_02]
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.7.Final.jar:]
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.7.Final.jar:]
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39) [weld-core-1.1.4.Final.jar:2011-11-22 20:01]
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-1.1.4.Final.jar:2011-11-22 20:01]
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    ... 25 more

看看this answer to a similar question我的第一个想法是请求没有经过FacesServlet。但是看看上面生成的HTML和堆栈跟踪中的form action字段,它看起来像请求通过FacesServlet。

有关于此的任何想法吗?我没得到什么?

1 个答案:

答案 0 :(得分:-1)

@SessionScoped注释是从javax.faces.bean包还是javax.enterprise.context导入的?您从不使用 javax.faces.bean包注释,因为它会为您提供除CDI之外的其他容器,并且只会粘贴到javax.enterprise.context