gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:58:06 26 4
gpt4 key购买 nike

我有一个 Facelet,它有两个 <h:forms> .一种形式的命令链接可以毫无问题地执行。其他形式的命令链接不起作用:a 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。但是看着 form action上面生成的 HTML 和堆栈跟踪中的字段,在我看来请求正在通过 FacesServlet。

对此有什么想法吗?我没有得到什么?

最佳答案

问题是我从 EJB 层调用 FacesContext.getCurrentInstance()。这就是为什么它在部署为 WAR 而不是 EAR 时起作用的原因。当时我的 Java EE 学习曲线很低:-)

关于java - FacesContext.getCurrentInstance() 在 FacesServlet(?) 处理的请求中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9136108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com