【问题标题】:SimpleJSFNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandlerSimpleJSFNavigationHandler 不能强制转换为 javax.faces.application.ConfigurableNavigationHandler
【发布时间】:2017-06-02 10:25:17
【问题描述】:

我正在将一个 JSF 1.2 项目迁移到具有 Ultima 布局的 JSF 2 和 PrimeFaces 6。 使用 Ultima 布局时,出现以下异常:

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

}

【问题讨论】:

    标签: jsf primefaces jsf-2 navigation


    【解决方案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);
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-03
      • 2019-06-09
      • 2013-07-21
      • 2016-09-26
      • 1970-01-01
      • 2013-03-20
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多