Jsf 2 如何在托管Bean中处理导航?

Jsf 2 如何在托管Bean中处理导航?,jsf-2,navigation,managed-bean,url-redirection,Jsf 2,Navigation,Managed Bean,Url Redirection,我有一个JSF2.1和Primefaces移动项目,应该在桌面和移动平台上运行。我编写了一个renderkit处理程序,它根据运行web应用程序的设备提供所需的renderkit。我很难将桌面页面重定向到移动页面 用于访问的URL:http://localhost:8080 我的应用程序结构是: root - WebContent --desktop ---login.xhtml --mobile ---login.xhtml --WEB-INF

我有一个JSF2.1和Primefaces移动项目,应该在桌面和移动平台上运行。我编写了一个renderkit处理程序,它根据运行web应用程序的设备提供所需的renderkit。我很难将桌面页面重定向到移动页面

用于访问的URL
http://localhost:8080

我的应用程序结构是

root
 - WebContent
   --desktop
     ---login.xhtml
   --mobile
     ---login.xhtml
   --WEB-INF
     ---web.xml
     ---faces-config.xml
   --META-INF
faces config.xml

<application>
        <view-handler>com.renderkit.CustomViewHandler</view-handler>
    </application>
    <navigation-rule>
                <display-name>Desktop-Login</display-name>
                <from-view-id>/desktop/login.xhtml</from-view-id>
                <navigation-case>
                    <from-outcome>MOBILE</from-outcome>
                    <to-view-id>/mobile/login.xhtml</to-view-id>
                </navigation-case>
            </navigation-rule>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AnalyzerUI</display-name>
  <welcome-file-list>
    <welcome-file>/desktop/login.xhtml</welcome-file>
  </welcome-file-list>
  <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>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.faces</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
    <context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>
@ManagedBean
@SessionScoped
public class User implements Serializable
{
private String name;
    private String password;

@PostConstruct
    public void myPostConstruct()
    {
        String renderKitId = FacesContext.getCurrentInstance().getViewRoot().getRenderKitId();
        System.out.println(" renderKitId >>> " + renderKitId);
        if (renderKitId.equalsIgnoreCase("PRIMEFACES_MOBILE"))
        {

            try
            {
                FacesContext.getCurrentInstance().getApplication(). getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null , "MOBILE");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
//getters and setters
}
CustomViewHandler

public class CustomViewHandler extends ViewHandlerWrapper
{
    private ViewHandler wrapped;

    public CustomViewHandler(ViewHandler wrapped)
    {
        this.wrapped = wrapped;
    }

    @Override
    public ViewHandler getWrapped()
    {
        return this.wrapped;
    }

    @Override
    public String calculateRenderKitId(FacesContext context)
    {
        HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
        String userAgent = req.getHeader("user-agent");
        String accept = req.getHeader("Accept");
        System.out.println("userAgent ::: "+ userAgent+ "   accept :: "+accept);
        if (userAgent != null && accept != null) {
            UAgentInfo agent = new UAgentInfo(userAgent, accept);
            if (agent.isMobileDevice()) {
                return "PRIMEFACES_MOBILE";
            }
        }

        return this.wrapped.calculateRenderKitId(context);
    }
}

你怎么回事啊?问题:我无法从desktop/login.xhtml重定向到mobile/login.xhtml;场景:基于renderkit,将显示相应的登录页面。is
renderkiti.equalsIgnoreCase(“PRIMEFACES\u MOBILE”)==true
?如果是这样的话,这可能是一个试图转发请求太晚的问题。您可以尝试在执行导航的行之前添加
FacesContext.getCurrentInstance().responseComplete()
。或者,您可以只使用
事件来处理重定向。这里的关键字是redirect,默认情况下,
NavigationHandler
执行转发。一个重定向IMO,是cleanerThanks很多@kolossus,我尝试使用它的工作现在。