从Facelets错误页面引用CDI托管bean [英] Referencing CDI managed bean from Facelets error page
问题描述
我很难让通用错误页面在使用JSF 2,Facelets和CDI的(非常简单的)WAR项目中工作.
I'm having a hard time trying to get a generic error-page to work in a (really simple) WAR project that uses JSF 2, Facelets and CDI.
我的应用程序服务器是WebLogic 12c,它应支持所有现成的功能,但是无法显示错误页面.当我将完全相同的WAR部署到Glassfish应用程序服务器时,它可以工作.
My application server is WebLogic 12c, which should support all of this out-of-the-box, but it fails to show the error page. When I deploy the exact same WAR to a Glassfish application server, it works.
我倾向于将WebLogic归咎于CDI部门的错误,但可以使用一些额外的专业知识来确定我的方法是否错误.
I'm leaning towards blaming WebLogic for being buggy in the CDI department, but could use some additional expertise to see if my approach is wrong.
这是我的应用程序要证明的内容:
Here's what my application is trying to proof:
执行重定向后,可以从错误页面引用一个已经存在的CDI托管Bean
One can reference an already existing CDI managed bean from an error page after a redirect to it has been performed
项目包含一个单独的@SessionScoped
受管bean,用于生成在 welcome 页和 error 页上显示的内容.
The project holds a single @SessionScoped
managed bean that is used to generates content that is shown on both the welcome page as well as the error page.
欢迎页面显示@SessionScoped
bean生成的内容,并使用@RequestScoped
bean抛出(Runtime)?Exception
(这反过来会引发到错误页面的重定向).
The welcome page displays content generated by the @SessionScoped
bean, and uses a @RequestScoped
bean to just throw a (Runtime)?Exception
(which in turn initiates a redirect to the error page).
这是我的相关代码:
-
web/WEB-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
web/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/faces/error.xhtml?faces-redirect=true</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/error.xhtml?faces-redirect=true</location>
</error-page>
</web-app>
web/error.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html">
<h:head>
<title>Error page</title>
</h:head>
<h:body>
<h1>You are now on the error page</h1>
<h3>Current milliseconds: #{sessionScopedBean.milliseconds}</h3>
</h:body>
</html>
web/index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html">
<h:head>
<title>Generate exception</title>
</h:head>
<h:body>
<h1>Please generate an exception of some sorts</h1>
<h1>Current milliseconds: #{sessionScopedBean.milliseconds}</h1>
<h:form>
<h:commandButton action="#{exceptionGeneratingBean.generateException()}" value="Exception" />
<h:commandButton action="#{exceptionGeneratingBean.generateRuntimeException()}" value="RuntimeException" />
</h:form>
</h:body>
</html>
src/java/nl/rensvanleeuwen/bean/ExceptionGeneratingBean.java
package nl.rensvanleeuwen.bean;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@RequestScoped
@Named
public class ExceptionGeneratingBean {
public void generateRuntimeExcetion() {
throw new RuntimeException("Here is the exception!");
}
public void generateException() throws Exception {
throw new Exception("Here is the exception!");
}
}
src/java/nl/rensvanleeuwen/bean/SessionScopedBean.java
package nl.rensvanleeuwen.bean;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class SessionScopedBean implements Serializable {
public long getMilliseconds() {
return System.currentTimeMillis();
}
}
将其部署到Glassfish 3.1.2时,当按下其中一个按钮时,会得到以下输出:
When I deploy this to Glassfish 3.1.2, I get the following output when one of the buttons is pressed:
另一方面,在WebLogic上,它崩溃了,并显示以下堆栈跟踪:
On WebLogic, on the other hand it blows up, and the following stacktrace is shown:
####<Aug 30, 2013 11:24:26 PM CEST> <Error> <HTTP> <rens7> <131Server> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1377897866330> <BEA-101107> <[ServletContext@2045981067[app:_appsdir_ErrorPageTest_war module:ErrorPageTest.war path:null spec-version:3.0]] Problem occurred while serving the error page.
javax.servlet.ServletException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:338)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:221)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:564)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:263)
at weblogic.servlet.internal.ForwardAction.run(ForwardAction.java:22)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.ErrorManager.handleException(ErrorManager.java:144)
at weblogic.servlet.internal.WebAppServletContext.handleThrowableFromInvocation(WebAppServletContext.java:2239)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1514)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
Caused By: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:612)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:71)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
at nl.rensvanleeuwen.bean.SessionScopedBean$Proxy$_$$_WeldClientProxy.getMilliseconds(SessionScopedBean$Proxy$_$$_WeldClientProxy.java)
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:305)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:138)
at com.sun.el.parser.AstValue.getValue(AstValue.java:184)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:150)
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
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 weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:338)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:221)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:564)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:263)
at weblogic.servlet.internal.ForwardAction.run(ForwardAction.java:22)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.ErrorManager.handleException(ErrorManager.java:144)
at weblogic.servlet.internal.WebAppServletContext.handleThrowableFromInvocation(WebAppServletContext.java:2239)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1514)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
>
我无法使此构造工作.有人对我可能忽略的东西有任何指示吗?
I haven't been able to make this construct work. Does anybody have any pointers to stuff that I might have overlooked?
推荐答案
现在在Oracle球场上.已针对WebLogic 12.1.1.0报告了错误17410908.
The ball is in the court of Oracle now. Bug 17410908 was reported against WebLogic 12.1.1.0.
这篇关于从Facelets错误页面引用CDI托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!