3

I have been using JSF for a few years now, but I still have doubts when it comes to deciding how to pass parameters to a target page/bean.

I do think this question is a bit complex, and that some may tell me to break it down into smaller questions. But, I also think that the answer to all of the questions bellow are related, and that it addresses the lack of intuitiveness when all you want JSF to do is: "Go to that page and pass this as a parameter".

  1. First, how to decide between Forward and Redirect?
  2. After that, how to choose between h:commandLink/h:commandButton, h:link or h:outputLink?
  3. Then, combined with the option I choose above, should I use f:param or f:setPropertyActionListener? Will both properly pass parameters to the target bean, independently of its scope?
  4. Finally, on the target bean/page, when should I use f:viewParam, or recover parameters from the request programmatically?
RinaldoDev
  • 985
  • 6
  • 21

1 Answers1

4

I'm going to answer your questions based on my own experience. Some of them are so open that more than one answer could fit.

A page forward is the way to go unless you explicitly require the browser url to be changed. A page forward is basically faster than a redirection as it requires less steps. A page redirect is required if you want to make your views bookmarkable.

Use <h:commandLink />/<h:commandButton /> only when you need to POST the server. Later on, you'll be able to perform a page forward or a redirection depending on what the method returns. As an example:

<h:commandLink action="#{bean.processForm}" value="Submit" />
public String processForm(){
    try{
        save();
        return "list";
    }
    catch(Excepcion e){
        addFacesMessage("Error saving");
        //Error saving the object, keep in the same view
        return null;
    }
}

Use <h:link outcome="list" value="Go to list" /> for pure page to page navigation within the JSF application. You can use either page forward and redirect. Use <f:param /> to pass view parameters.

<h:outputLink value="www.stackoverflow.com" /> could be used for external links to other sites (not into your application). Use <f:param /> to pass view parameters. I however prefer to use plain HTML with <a href="www.stackoverflow.com" /> myself for this case.

As for passing parameters to action methods in POST requests, you've got several options. f:setPropertyActionListener was so popular in JSF 1.x, but if you're already at 2.x I would recommend you going with EL 2.2, which allows method parameter declaration. Being able to use it depends on the application server you're using, but even if not available you could import yourself. Then, you'll be able to do things like that:

<h:commandButton action="#{bean.saveCar(currentCar)}" value="Save Car" />

Use it wherever you can, it'll make things just easier.

For the view parameters, use <f:viewParam /> too. It's the standard JSF way of parsing the parameters from the GET request, so just let the framework do the retrieving work for you!

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • What if you were going to forward from page A to page B, and both pages uses different beans. Would you use method parameter? If yes, calling a method on bean A or B? Or, would you use `f:setPropertyActionListener` and don't even call a method at all? Or, would you call a method and set parameters programmatically? Would you please update your answer with that information? – RinaldoDev Mar 09 '14 at 17:33
  • @RinaldoPJr you're misunderstanding the JSF cycle. One thing is to pass parameters to a view method which is actually tied to the current view. That's done by a POST request and is described in my answer. What you're asking for is how to pass parameters when performing a navigation. That could be perform in several ways, but what I recommend is to use `f:viewParam` in order to parse plain GET parameters. Have a look at [this](http://bit.ly/1fZDGLa). You could of course mix both utilities performing a POST with a parameter and sending the same parameter in your outcome. That's plain HTTP matter – Aritz Mar 10 '14 at 07:22
  • Sorry, I forgot to mention in my last comment that I was trying to think of something that wasn't GET parameters. You said, when performing a navigation, "That could be perform in several ways", would you mind to expand your answer with an explanation/examples? – RinaldoDev Mar 10 '14 at 12:05
  • [Here you have](http://stackoverflow.com/a/21277621/1199132) an answer I gave some time ago about the flash scope. This allows you passing non-view parameters through views. – Aritz Mar 10 '14 at 12:08