2011-04-12 64 views
1

繼我的previous question,我有一個問題,爲什麼它會拋出這個異常,如果我在faces-config.xml中註冊RedirectNavigationHandler後使用 from primefaces 。 :自定義NavigationHandler不能轉換爲javax.faces.application.ConfigurableNavigationHandler

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 

使用<p:commandButton>是好的,但不能如此,<p:button>

這裏是我的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> 

任何想法?

謝謝!

+3

延伸試試['ConfigurableNavigationHandler'(http://download.oracle.com/javaee/6/api/javax/faces/application/ConfigurableNavigationHandler。 html) – 2011-04-12 09:14:40

回答

4

對於JSF 2.0,您顯然應該擴展ConfigurableNavigationHandler

ConfigurableNavigationHandler延伸NavigationHandler合同允許NavigationCases組成規則庫進行導航的運行時檢查。符合引入此類(或更高版本)必須的規範版本的實現使其NavigationHandler是此類的擴展。

(重點煤礦)這是沒有提及的方式/警告在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; 
     } 
    } 

} 
+0

哇!謝謝 ! – bertie 2011-04-13 02:47:15