2017-01-18 35 views
0

我正在使用Ultima佈局將JSF 1.2項目遷移到JSF 2和PrimeFaces 6。 當使用終極佈局,我得到以下異常:SimpleJSFNavigationHandler不能轉換爲javax.faces.application.ConfigurableNavigationHandler

SimpleJSFNavigationHandler不能轉換爲javax.faces.application.ConfigurableNavigationHandler。

如何解決?

以下是第SimpleJSFNavigationHandler

import java.io.IOException; 

import javax.faces.FacesException; 
import javax.faces.application.NavigationHandler; 
import javax.faces.application.ViewHandler; 
import javax.faces.component.UIViewRoot; 
import javax.faces.context.ExternalContext; 
import javax.faces.context.FacesContext; 

import org.springframework.web.jsf.DecoratingNavigationHandler; 

public class SimpleJSFNavigationHandler extends DecoratingNavigationHandler { 


    public static final String DEFAULT_REDIRECT_PREFIX = "redirect:"; 
    public static final String DEFAULT_FORWARD_PREFIX = "/"; 

    private String redirectPrefix = DEFAULT_REDIRECT_PREFIX; 
    private String forwardPrefix = DEFAULT_FORWARD_PREFIX; 

    public SimpleJSFNavigationHandler() { 
    } 


    public SimpleJSFNavigationHandler(NavigationHandler originalNavigationHandler) { 
     super(originalNavigationHandler); 
    } 

    @Override 
    public void handleNavigation(FacesContext facesContext, String fromAction, String outcome, NavigationHandler originalNavigationHandler) { 
     if (outcome != null && outcome.startsWith(redirectPrefix)) { 
     ExternalContext externalContext = facesContext.getExternalContext(); 
     ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 
      String url = outcome.substring(redirectPrefix.length()); 
      String urlParams=""; 
      if(url.indexOf("?")>0) 
       urlParams = url.substring(url.indexOf("?")); 
      String redirectPath = viewHandler.getActionURL(facesContext, url); 
      try { 
       //System.out.println("MMMMMMMMMMMMMMMM:::::::::::::::::" + urlParams); 
       externalContext.redirect(externalContext.encodeActionURL(redirectPath+urlParams)); 

      } catch (IOException e) { 
       throw new FacesException(e.getMessage(), e); 
      } 
      facesContext.responseComplete(); 
     } else if (outcome != null && outcome.startsWith(forwardPrefix)) { 
     ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 
     //create new view 
     String newViewId = outcome.substring(forwardPrefix.length()); 
      if (newViewId.length()>0 && newViewId.charAt(0)!='/') { 
       newViewId = "/" + newViewId; 
      } 
     UIViewRoot viewRoot = viewHandler.createView(facesContext, newViewId); 
     viewRoot.setViewId(newViewId); 
     facesContext.setViewRoot(viewRoot); 
     facesContext.renderResponse(); 
     } else { 
     callNextHandlerInChain(facesContext, fromAction, outcome, originalNavigationHandler); 
     } 
    } 

} 

回答

1

你不需要Spring的DecoratingNavigationHandler。你可以使用JSF自己的ConfigurableNavigationHandlerWrapper

public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper { 

    private ConfigurableNavigationHandler wrapped; 

    public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) { 
     this.wrapped = wrapped; 
    } 

    @Override 
    public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) { 
     if (...) { 
      // Your original code here. 
     } else if (...) { 
      // Your original code here. 
     } else { 
      // Update only the last else part as below. 
      getWrapped().handleNavigation(facesContext, fromAction, outcome); 
     } 
    } 

    @Override 
    public ConfigurableNavigationHandler getWrapped() { 
     return wrapped; 
    } 

} 

在即將到來的JSF 2.3這甚至可以被進一步簡化爲每spec issue 1429應進一步減少FacesWrapper實現樣板代碼。

public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper { 

    public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) { 
     super(wrapped); 
    } 

    @Override 
    public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) { 
     if (...) { 
      // Your original code here. 
     } else if (...) { 
      // Your original code here. 
     } else { 
      // Update only the last else part as below. 
      getWrapped().handleNavigation(facesContext, fromAction, outcome); 
     } 
    } 

} 
相關問題