0

我有一个带有 Primefaces 移动项目的 JSF 2.1,它应该在桌面和移动平台上运行。我编写了一个渲染套件处理程序,它根据运行 Web 应用程序的设备提供所需的渲染套件。我很难将桌面页面重定向到移动页面。

用于访问的 URLhttp://localhost:8080

我的应用程序结构是

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

面孔-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>

web.xml

<?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>

用户类,用于 login.xhtml 的 Session 范围的托管 Bean 类

@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
}

自定义视图处理程序

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

0 回答 0