tags:

views:

11

answers:

1

Hello. Im new into JSF 2.0. On the last version i understand that if i want change rules about "what send to the client" i just need to configure the faces-config.xml.

Now, on version 2.0, how can manage the Action? For example, if i have this on a index.xhtml

<h:commandButton id="submit" value="submit" action="response" />

and i need to call a page called response.html (not xhtml) or that page placed into /folder/response.html, or somethings else? How can do it? I know JSF 2.0 is very flexible about these things (the concept of href links is beaten). So i think i can manage this with other methodologies, right?

+1  A: 

The action can point two things:

  1. A method expression action="#{bean.methodname}" where the method look like this:

    @ManagedBean
    @RequestScoped
    public class Bean {
        public String methodname() {
            // Do some business task here.
            return "response";
        }
    }
    

    After executing the method the action will effectively end up containing the return value of the method, like so: action="response".

    You can also control the outcome "dynamically" the usual Java way:

    public String methodname() {
        if (someCondition) {
            return "somepage";
        } else {
            return "anotherpage";
        }
    }
    

    Depending on the condition outcome, the action will end up like action="somepage" or action="anotherpage"

  2. Another XHTML page in the same folder as the current XHTML page. You just have to specify the filename: action="response".

Either way, it will go to the XHTML page which is composed by outcome + ".xhtml" where outcome is the action value (e.g. response.xhtml, somepage.xhtml or anotherpage.xhtml) which is supposed to be in the same folder as the XHTML file containing the h:commandButton.

You don't need to configure anything in faces-config.xml for this. Previously, during JSF 1.x ages you would need to define <navigation-case> for this.

See also:

BalusC
Thanks for the tip. Yeah, in the case "1" i call a method on a bean. But it just return a variable. How can say "return the page somepage.xhtml if the bean called return "some value" or "another value"? For example, if the bean return "hello" it must load helloworld.xhtml, if return "mate" it must load byemate.xhtml.
markzzz
Just make use of `if-else` statement in action method to return a different `String` outcome. See the second code block in updated answer.
BalusC
So maybe i don't understand the process of the JSF. That will "return" a string server-side from the bean, it won't call somepage.xhtml or anotherpage.xhtml right? Else, the webserver will evalutate first the bean and after this the whole "action" statment? :)
markzzz
It will. The only difference is that the return value is now controlled by a bean method. Just have `somepage.xhtml` and `anotherpage.xhtml` in the same folder and JSF will navigate to either of those pages depending on the outcome value.
BalusC
Ah ok. I always forgot the lifecycle of a JSF! I went from php methodology, and this is very insane at the moment :) thanks man
markzzz
You're welcome.
BalusC