jsf-2 - 从 Facelets 错误页面引用 CDI 托管 bean

标签 jsf-2 error-handling weblogic cdi facelets

我很难尝试让通用错误页面在使用 JSF 2、Facelets 和 CDI 的(非常简单的)WAR 项目中工作。

我的应用服务器是WebLogic 12c,开箱即用,应该支持所有这些,但是无法显示错误页面。当我将完全相同的 WAR 部署到 Glassfish 应用程序服务器时,它可以正常工作。

我倾向于指责 WebLogic 在 CDI 部门的错误,但可以使用一些额外的专业知识来查看我的方法是否错误。

这是我的应用程序试图证明的内容:

执行重定向后,可以从错误页面引用已存在的 CDI 托管 bean

该项目包含一个 @SessionScoped 托管 bean,用于生成在欢迎 页面和错误 页面上显示的内容em> 页。

欢迎页面显示由 @SessionScoped bean 生成的内容,并使用 @RequestScoped bean 来抛出一个 (Runtime)?Exception(这又会启动到错误页面的重定向)。

这是我的相关代码:


  • 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 时,按下其中一个按钮时我得到以下输出:


您现在在错误页面上

当前毫秒数:1377897262568


另一方面,在 WebLogic 上它爆炸了,并显示了以下堆栈跟踪:

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

我没能使这个构造起作用。有人对我可能忽略的东西有任何指示吗?

最佳答案

现在球在甲骨文的球场上。针对 WebLogic 12.1.1.0 报告了错误 17410908。

关于jsf-2 - 从 Facelets 错误页面引用 CDI 托管 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18542209/

相关文章:

jsf-2 - JBoss 7.1.1 中的嵌套复合组件损坏

asp.net-mvc - NerdDinner Visual Web Developer 2010设置

python - Python TypeError即使我仅使用int和float

java - 具有 http 传输的嵌入式 JMS 代理

在 WLS 12 中部署应用程序时出现 java.lang.ArrayIndexOutOfBoundsException

jpa - 升级 GlassFish 3.1.2.2 以使用 JSF 2.2

jsf-2 - 如何设置 primefaces 时钟小部件的开始时间?

jsf-2 - RichFaces 中的 session 空指针

c# - 无法连接kafka集群时无异常

properties - 带有 Weblogic 的外部属性文件