Seam and redirect-after-post

From Shrubbery

Jump to: navigation, search


Seam reference: Configuring Redirects

What is redirect-after-post?

See Redirect-after-post

Redirect-after-post and Seam conversations

Normal Faces postbacks (commandLink and commandButton) will propagate a long running conversation to the next request. If the navigation rule has 'redirect' defined then the Seam RedirectFilter must be enabled in web.xml in order to have the long running conversations propagate across the redirect.

Let's say we want to create a link to a page that has a long running conversation with a SFSB in it. This SFSB will hold some state across perhaps several pages.

The first step is to put the link on the page that starts the page flow:

        <h:form>
            <p>Starting a pageflow and a conversation with an ordinaruy method call.
            This one uses a JSF <tt>redirect</tt> in navigation.xml...</p>
            <h:commandLink action="#{testBean.startPageflow}" value="Start conversation"/>
        </h:form>

The action method in the bean needs to have the @Begin notation on it which will promote the current conversation to a long running one (thereby preserving the instance of TestBean in the conversation):

@Name("testBean")
@Stateful
public class TestBean implements TestLocal
{
    @Begin
    public String startPageflow()
    {
        return "first";
    }

  
    @Destroy
    @Remove
    public void destroy()
    {
        // This should not be called until the end.
        System.out.println("TestBean destroyed");
    }
}

We then define the navigation rule in 'navigation.xml' (faces navigation rule style):

    <navigation-rule>
        <from-view-id>/pagewithlink.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>first</from-outcome>
            <to-view-id>/first.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
    </navigation-rule>

... or the Seam pages.xml style of navigation:

  <page view-id="/pagewithlink.xhtml">
    <navigation>
      <rule if-outcome="first">
        <redirect view-id="/first.xhtml"/>
      </rule>
    </navigation>
  </page>

The <redirect/> element will cause JSF to emit a redirect to the browser rather rendering the page 'first.xhtml' after the startPageflow method is called. The browser will redirect to the next URL and 'first.xhtml' will be rendered in a separate request. Seam's RedirectFilter will add parameters to the URL that will cause Seam to locate the conversation created before the redirect and activate it. If the RedirectFilter is not configured then a new temporary conversation will be created on the next page and if that page references TestBean, a new instance will be created which will not be initialized. The temporary conversation will only last for the rendering of the next page and the second instance of TestBean will be destroyed along with it.

Enabling the Seam RedirectFilter

In Seam 1.2.1.GA, the SeamFilter is the master and it invokes all the sub-filters, which are registered as Seam components. So, if you have SeamFilter in WEB-INF/web.xml, the redirect support is already there.



In earlier versions of Seam, the redirect filter was separately configured in WEB-INF/web.xml. DO NOT use this with Seam 1.2.1 GA an beyond because the filter will be applied more than once! Here is an example of how it used to be configured:

   <filter>
       <filter-name>Seam Redirect Filter</filter-name>
       <filter-class>org.jboss.seam.web.RedirectFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>Seam Redirect Filter</filter-name>
       <url-pattern>*.seam</url-pattern>
   </filter-mapping>
Personal tools