JSF - change action for h: commandButton (2.0)

Im new in JSF 2.0. In the latest version, I understand that if I need change rules about what to send to the client, I just need to configure faces-config.xml.

Now, on version 2.0, how can you control the action? For example, if I have this on index.xhtml

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

and do I need to call a page called response.html (not xhtml) or this page, placed in /folder/response.html, or something else? How can I do that? I know that JSF 2.0 is very flexible about these things (the href link concept is beaten up). So I think I can handle other methodologies, right?

+3
source share
1 answer

action can indicate two things:

  • Method expression action="#{bean.methodname}" , where the method looks like this:

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

    After the method is executed, the action will end effectively, containing the return value of the method, for example: action="response" .

    You can also control the result “dynamically” in the usual way of Java:

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

    Depending on the result of the condition, the action will look like action="somepage" or action="anotherpage"

  • The other XHTML page in the folder is the same as the current XHTML page. You just need to specify the file name: action="response" .

In any case, it will go to the XHTML page, which consists of outcome + ".xhtml" , where outcome is the value of the action (for example, response.xhtml , somepage.xhtml or anotherpage.xhtml )), which should be in the same as An XHTML file containing h:commandButton .

You do not need to configure anything in faces-config.xml . Earlier during JSF 1.x you would need to define <navigation-case> for this.

See also:

+6
source

Source: https://habr.com/ru/post/886233/


All Articles