Referencing CDI managed bean from Facelets error page

谁说我不能喝 提交于 2020-01-04 04:12:06

问题


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.

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.

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:

One can reference an already existing CDI managed bean from an error page after a redirect to it has been performed

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.

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).

Here's my relevant code:


  • 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();
        }
    }
    

When I deploy this to Glassfish 3.1.2, I get the following output when one of the buttons is pressed:


You are now on the error page

Current milliseconds: 1377897262568


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?


回答1:


The ball is in the court of Oracle now. Bug 17410908 was reported against WebLogic 12.1.1.0.



来源:https://stackoverflow.com/questions/18542209/referencing-cdi-managed-bean-from-facelets-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!