Search code examples
jsf-2richfacesservlet-filters

Servlet filtering not applying on JSF navigation rule


We have servlet filter which verifies couple of session variables before JSF view display. Some how when a navigation rule is invoked in bean using handleNavigation(...) method, filter is not being called. Am I missing anything? Any help would be appreciated.

Here is 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>

And navigation rule is faces-config.xml:

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

And here is code bean invoking:

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

Thank you for your time!


Solution

  • You mean that you expect the filter to be called for nextPage.xhtml as well? JSF navigation doesn't perform a request, forward or include. It just creates a new view within the very same request which is then to be rendered.

    If you need to create a brand new request, call ExternalContext#redirect() instead.

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

    Or if you're actually inside an action method, return the navigation outcome with ?faces-redirect=true:

    public String submit() {
        // ...
    
        return "/nextPage.xhtml?faces-redirect=true";
    }
    

    Or as an alternative, depending on the concrete functional requirement, use a ViewHandler instead of a Filter so that you can hook on createView().