Differences between revisions 8 and 9
Revision 8 as of 2006-06-06 10:47:25
Size: 2699
Editor: relay
Comment: tyop
Revision 9 as of 2009-09-20 23:02:05
Size: 2699
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Custom Navigation Handler

MyFaces and Sun RI implementations present two methods of navigating between pages:

  1. Forwarding.
  2. Redirection.

The first method is not good, since forwarding a request does not inform the client about the new forwarded url. This causes the following problems:

  1. It is not nice (some may say professional) to see a url which is not the actual current url.
  2. When reloading page, the previous page is loaded.

It seems to me, that only for these two minor issues, most companies with a serious web application, will prefer using the redirection method over forwarding. Although, redirection is less efficient, since there are two requests instead of one.

In addition, when using forwarding technique with JSF, the programmer will probably pass parameters with the following methods:

  1. As a parameter tag (f:param).
  2. Set a request scope value.

This causes the following problems:

  1. There are two ways of passing parameters between pages: request parameter, request scope value. Parameters handling is better treated in a unified manner, otherwise it will be harder to deal with a page that is accessed from a lot of different places.
  2. The values in the second method are not passed when using redirection.

For these reasons (And also for the reason of solving complex dynamic navigation issues), assuming redirection is the preferable way of navigating between pages, one should consider using a custom Navigation Handler adding the following capabilities:

  1. Custom dynamic redirection to any page: This simply allows to return an action result which will cause the Navigation Handler to directly redirect to a page. For example, in order to redirect to a url:

  function String saveAction()
  {
    ..
    ..
    return "redirect:save_ok.jsp"
  }
  • Pay attention, that in order to directly redirect to a specific page the prefix: "redirect:" was added. Also pay attention to the fact that only the relative path is used.
  1. Dynamic Url parameters passing: This allows passing parameters between pages even when using redirection method. For exmaple, in order to navigate from a pages name: view.jsp to a page named: edit.jsp, with the parameter: objectId, the following thing can be done in navigation rule code:

  <navigation-rule>
    <from-view-id>/view.jsp</from-view-id>
    <navigation-case>
      <from-outcome>edit</from-outcome>
      <to-view-id>/view.jsp?objectId=#{view.objectId}</to-view-id>
      <redirect />
    </navigation-case>
  </navigation-rule>
  • In view.jsp file the command link looks like:

  <h:commandLink value="Edit" action="edit" />

Custom_Navigation_Handler (last edited 2009-09-20 23:02:05 by localhost)