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?
해결책
The action
can point two things:
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"
oraction="anotherpage"
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:
- We don't need no stinkin' faces-config.xml! - blog by lead Mojarra developer.