2

At the moment I am using the following code:

public void init() {
    question = questionBean.findQuestion(questionParamId);
}

Which is invoked by this:

<f:metadata>
    <f:viewParam name="id" value="#{questionShowBackingBean.questionParamId}" />
    <f:event type="preRenderView" listener="#{questionShowBackingBean.init}" />
</f:metadata>

So the URL is: http://www.mycompany.com/show.xhtml?id=8

Now I have begun using PrettyFaces and I have seen the <action> element in the URL-mapping element I wonder if I could have written <action>#{questionShowBackingBean.init}</action> instead?

If so should I remove metadata element then, or should I use that instead because it may in the future change from using PrettyFaces? Last, where does the invocation in the action element occur? Does it occur before the listener I have now?

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434

1 Answers1

2

The <f:event> and <action> tags seem very similar, but have a few large differences that might influence your decision:

  1. <action> allows you to perform navigation by returning a navigation string.
  2. <f:event> is native JSF and lets you stay portable if you want to switch URL-rewriting tools in the future.
  3. <action> lets you choose which phase to invoke the action via the <action phaseId="..." /> attribute. <f:event> usually always invokes at the same time during RENDER_RESPONSE, after you really need it to invoke if you are using this information in JSF action method or in the INVOKE_APPLICATION phase.

By default, <action>s are invoked after the RESTORE_VIEW phase, but as I said before, you can control this yourself.

Usually I prefer to use <action> because it works without the need for any <f:viewParam> elements, and it also lets me navigate before any lifecycle processing has occurred (keeping things a bit more secure).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Lincoln
  • 3,151
  • 17
  • 22
  • How would you bind the path parameters then? Setting them directly on the bean? Or can you pass them as argument to the action method? – LuckyLuke Nov 26 '11 at 16:18
  • I would bind the path parameters by using PrettyFaces injection, as you suggested. – Lincoln Nov 26 '11 at 16:26