Servlet过滤不适用于JSF导航规则

时间:2012-04-23 22:02:35

标签: jsf-2 richfaces servlet-filters

我们有servlet过滤器,可以在JSF视图显示之前验证几个会话变量。一些如何在使用handleNavigation(...)方法在bean中调用导航规则时,不会调用过滤器。我错过了什么吗?任何帮助将不胜感激。

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
         version="2.5">

    <display-name>JSF</display-name>
    <description>
        JSF
    </description>
    <filter>
        <filter-name>myFilter</filter-name>
        <filter-class>myFilterClassPath</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>myFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>pathtoAppServletClass</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <!-- Welcome File List -->
    <welcome-file-list>
        <welcome-file>welcome.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

导航规则是faces-config.xml:

<navigation-rule>
        <navigation-case>
            <from-outcome>nextPage</from-outcome>
            <to-view-id>/nextPage.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

这是代码bean调用:

FacesContext context = FacesContext.getCurrentInstance();
      context.getApplication().getNavigationHandler().handleNavigation(context, null, "nextPage");

感谢您的时间!

1 个答案:

答案 0 :(得分:1)

您的意思是您希望为nextPage.xhtml调用过滤器吗? JSF导航不执行请求,转发或包含。它只是在同一个请求中创建一个新视图,然后再呈现。

如果您需要创建全新的请求,请改为呼叫ExternalContext#redirect()

externalContext.redirect(externalContext.getRequestContextPath() + "/nextPage.xhtml");

或者,如果您实际上在操作方法中,请使用?faces-redirect=true返回导航结果:

public String submit() {
    // ...

    return "/nextPage.xhtml?faces-redirect=true";
}

或者作为替代方案,根据具体的功能要求,使用ViewHandler而不是Filter,以便您可以挂钩createView()