webtier@glassfish.java.net

Use f:metadata to navigate to appropriate page upon GET request?

From: <webtier_at_javadesktop.org>
Date: Fri, 31 Jul 2009 18:22:31 PDT

Consider a user making a GET request to the index.xhtml page of my app. Suppose I want to go to automatically navigate to login.xhtml or main.xhtml, depending on whether the user needs to log in or not. (They might have bookmarked index.xhtml and visited it again after already being logged in...)

I figured out how to do this with JSF 2.0, but I have a couple of questions. Here is the approach:

1) Add this on top of the h:head tag (or maybe instead--I haven't tried that out)

<f:metadata>
  <f:viewParam name="bogus" value="fubar"/> <!-- See section 2.2.1 of the JSF 2.0 spec -->
  <f:event type="preRenderView" listener="#{user.checkLogin}"/>
</f:metadata>

2) In the user bean, add this method:

   public void checkLogin(ComponentSystemEvent event) throws AbortProcessingException {
      String next;
      if (loggedIn) next = "main"; else next = "login";
      FacesContext context = FacesContext.getCurrentInstance();
      ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
      handler.performNavigation(next);
   }

This works, but

1) Is it legitimate? I got lost in the fog, trying to figure out whether I am allowed to navigate to a new view in this situation.

2) Why does the spec have the seemingly bizarre requirement that there is at least one viewParam child? In this case, I need none. I also understand that f:metadata was intended to be a fairly general mechanism that could be extended later. Why not simply use the fact that there is a f:metadata, or that it has at least one child of some type, to invoke the other lifecycle phases (on the f:metadata children)?

Thanks,

Cay
[Message sent by forum member 'cayhorstmann' (cayhorstmann)]

http://forums.java.net/jive/thread.jspa?messageID=358438