Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

jsf 2 - JSF - Change the action for a h:commandButton (2.0)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...