8.3. Starting conversations with GET requests

JSF does not define any action listener triggered when a page is accessed via a non-Faces request (a HTTP GET request, for example). This can occur when a user bookmarks the page, or navigates to the page via an <h:outputLink>.
Sometimes we want a conversation to begin immediately the page is accessed. Since there is no JSF action method, we cannot annotate the action with @Begin.
Further problems arise when the page requires state to be fetched into a context variable. We have already seen two methods of solving this problem. If the state is held in a Seam component, we can fetch the state in a @Create method. If not, we can define a @Factory method for the context variable.
If neither option works for you, Seam lets you define a page action in the pages.xml file.
<pages> 
  <page view-id="/messageList.jsp" action="#{messageManager.list}"/>
  ... 
</pages>
This action method is called at the beginning of the render response phase — that is, any time the page is about to be rendered. If a page action returns a non-null outcome, Seam will process any appropriate JSF and Seam navigation rules. This can result in a completely different page rendering.
If beginning a conversation is all you want to do before rendering the page, you can use a built-in action method:
<pages> 
  <page view-id="/messageList.jsp" action="#{conversation.begin}"/> 
  ... 
</pages>
You can also call this built-in action from a JSF control, and that #{conversation.end} similarly ends conversations.
The <begin-conversation> element can be used as follows for further control over joining existing conversations, or beginning a nested conversation, a pageflow, or an atomic conversation.
<pages> 
  <page view-id="/messageList.jsp"> 
    <begin-conversation nested="true" pageflow="AddItem"/> 
  <page> 
  ... 
</pages>
There is also an <end-conversation> element.
<pages> 
  <page view-id="/home.jsp"> 
    <end-conversation/> 
  <page> 
  ...
</pages>
We now have five options to begin a conversation immediately the page is accessed:
  • Annotate the @Create method with @Begin
  • Annotate the @Factory method with @Begin
  • Annotate the Seam page action method with @Begin
  • Use <begin-conversation> in pages.xml.
  • Use #{conversation.begin} as the Seam page action method