0

我们有 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");

感谢您的时间!

4

1 回答 1

1

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

如果您需要创建一个全新的请求,请致电ExternalContext#redirect()

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

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

public String submit() {
    // ...

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

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

于 2012-04-23T22:27:38.407 回答