自定义 NavigationHandler 无法转换为 javax.faces.application.ConfigurableNavigationHandler

发布于 2024-11-01 05:31:36 字数 7112 浏览 0 评论 0原文

根据我的上一个问题,我有一个问题,为什么这个异常是如果我在 faces-config.xml 中注册 RedirectNavigationHandler 后使用 primefaces 中的 ,则会抛出异常。 :

SEVERE: Error Rendering View[/TBrowse.xhtml]
java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler
        at org.primefaces.component.button.ButtonRenderer.findNavigationCase(ButtonRenderer.java:114)
        at org.primefaces.component.button.ButtonRenderer.buildOnclick(ButtonRenderer.java:90)
        at org.primefaces.component.button.ButtonRenderer.encodeMarkup(ButtonRenderer.java:56)
        at org.primefaces.component.button.ButtonRenderer.encodeEnd(ButtonRenderer.java:38)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
        at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
        at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
        at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:177)
        at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:120)
        at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:61)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)
        at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)
        at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:229)
        at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:152)
        at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:75)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
        at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
        at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
        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:313)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
unhandled exception : java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler
cause exception : java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler, cause exception is BE : false

使用 可以,但使用 则不然。

这是我的 faces-config.xml 摘录:

<application>
   ...
   <navigation-handler>id.co.sofcograha.RedirectNavigationHandler</navigation-handler>
   ...
</application>

和代码:

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

public class RedirectNavigationHandler extends NavigationHandler {
    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (outcome != null && !outcome.trim().equals("") && !outcome.endsWith("?faces-redirect=true")) {
            System.out.println("adding faces redirect for " + outcome);
            outcome += "?faces-redirect=true";
        }

        parent.handleNavigation(context, from, outcome);        
    }
}

这是 Maven 依赖项:

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>2.2.1</version>
</dependency>

对此事有什么想法吗?

谢谢 !

Following my previous question, i have a question about why is it this exception is being thrown if i use <p:button> from primefaces after registering the RedirectNavigationHandler in the faces-config.xml. :

SEVERE: Error Rendering View[/TBrowse.xhtml]
java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler
        at org.primefaces.component.button.ButtonRenderer.findNavigationCase(ButtonRenderer.java:114)
        at org.primefaces.component.button.ButtonRenderer.buildOnclick(ButtonRenderer.java:90)
        at org.primefaces.component.button.ButtonRenderer.encodeMarkup(ButtonRenderer.java:56)
        at org.primefaces.component.button.ButtonRenderer.encodeEnd(ButtonRenderer.java:38)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
        at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
        at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
        at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:177)
        at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:120)
        at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:61)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)
        at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)
        at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:229)
        at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:152)
        at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:75)
        at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
        at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
        at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
        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:313)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
unhandled exception : java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler
cause exception : java.lang.ClassCastException: my.package.RedirectNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler, cause exception is BE : false

Using <p:commandButton> is fine, but not so with <p:button>.

Here's my faces-config.xml excerpt :

<application>
   ...
   <navigation-handler>id.co.sofcograha.RedirectNavigationHandler</navigation-handler>
   ...
</application>

And the code :

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

public class RedirectNavigationHandler extends NavigationHandler {
    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (outcome != null && !outcome.trim().equals("") && !outcome.endsWith("?faces-redirect=true")) {
            System.out.println("adding faces redirect for " + outcome);
            outcome += "?faces-redirect=true";
        }

        parent.handleNavigation(context, from, outcome);        
    }
}

Here's the maven dependencies :

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>2.2.1</version>
</dependency>

Any ideas on this matter ?

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-11-08 05:31:36

对于 JSF 2.0,您显然应该扩展 ConfigurableNavigationHandler 代替。

ConfigurableNavigationHandler 扩展了 NavigationHandler 的协定,以允许对构成导航规则库的 NavigationCases 进行运行时检查。符合引入此类的规范版本(或更高版本)的实现必须使其NavigationHandler成为此类的扩展。< /p>

(强调我的)顺便说一下,NavigationHandler javadoc,所以我完全忽略了您上一个问题中的这一点。

您可以这样做:

package com.example;

import java.util.Map;
import java.util.Set;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.NavigationCase;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

public class RedirectNavigationHandler extends ConfigurableNavigationHandler {

    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (!outcome.endsWith("?faces-redirect=true")) {
            outcome += "?faces-redirect=true";
        }

        parent.handleNavigation(context, from, outcome);        
    }

    @Override
    public NavigationCase getNavigationCase(FacesContext context, String fromAction, String outcome) {
        if (parent instanceof ConfigurableNavigationHandler) {
            return ((ConfigurableNavigationHandler) parent).getNavigationCase(context, fromAction, outcome);
        } else {
            return null;
        }
    }

    @Override
    public Map<String, Set<NavigationCase>> getNavigationCases() {
        if (parent instanceof ConfigurableNavigationHandler) {
            return ((ConfigurableNavigationHandler) parent).getNavigationCases();
        } else {
            return null;
        }
    }

}

For JSF 2.0 you're apparently supposed to extend ConfigurableNavigationHandler instead.

ConfigurableNavigationHandler extends the contract of NavigationHandler to allow runtime inspection of the NavigationCases that make up the rule-base for navigation. An implementation compliant with the version of the specification in which this class was introduced (or a later version) must make it so that its NavigationHandler is an extension of this class.

(emphasis mine) This is by the way not mentioned/warned in NavigationHandler javadoc, so I completely overlooked that bit in your previous question.

Here's how you could do it:

package com.example;

import java.util.Map;
import java.util.Set;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.NavigationCase;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

public class RedirectNavigationHandler extends ConfigurableNavigationHandler {

    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (!outcome.endsWith("?faces-redirect=true")) {
            outcome += "?faces-redirect=true";
        }

        parent.handleNavigation(context, from, outcome);        
    }

    @Override
    public NavigationCase getNavigationCase(FacesContext context, String fromAction, String outcome) {
        if (parent instanceof ConfigurableNavigationHandler) {
            return ((ConfigurableNavigationHandler) parent).getNavigationCase(context, fromAction, outcome);
        } else {
            return null;
        }
    }

    @Override
    public Map<String, Set<NavigationCase>> getNavigationCases() {
        if (parent instanceof ConfigurableNavigationHandler) {
            return ((ConfigurableNavigationHandler) parent).getNavigationCases();
        } else {
            return null;
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文