6

I'm new to JSF and am trying to understand what the difference is between the action vs the outcome attributes? This is not the difference between buttons and command buttons, but between outcome vs action attributes on the same button tag.

For instance, I can have:

<h:button value="Go" outcome="<value>" />

or

<h:button value="Go" action="<value>" />

I'm not sure when to use which one. Also, does it make a difference if <value> is <#{bean.methodThatReturnsStringInNavigationRule}> or <string literal in navigation rule>?

Thank you.

dev
  • 2,949
  • 5
  • 37
  • 48
  • 1
    Buttons are typically used for navigation, while command buttons are typically used to perform business actions (and, if necessary, navigation). – skuntsel Feb 23 '13 at 08:09
  • 1
    As per your update, no one JSF component has both attributes in the same tag. The `` doesn't support `action` attribute at all. The `action` attribute is only supported in ``. – BalusC Feb 26 '13 at 17:54
  • The title of this question makes it much more easy to find now. I don't think it should be considered duplciate anymore. – bjedrzejewski Jun 19 '14 at 13:13

2 Answers2

15

Difference is that while action defines a server method to be executed, outcome specifies a view-id which will be destination of your page. You must use JSF inputs depending on the goal you're trying to achieve:

  • <h:button outcome="user-management"> targets you to the user management page. Imagine it as kind of link.
  • <h:commandButton action="#{backingBean.goToUserManagement}" If you return "user-management" in your action method, is doing the same as the outcome but it allows you to execute some logic into the server side. It must be embedded into a h:form tag.

Also there's no difference between <h:button outcome="user-management"> or <h:button outcome="#{backingBean.userManagementNavigationResult}">, as far as your server side getter method returns "user-management" value.

Aritz
  • 30,971
  • 16
  • 136
  • 217
4

If you see JSF 2.0 API there is no action attribute for <h:button> tag. <h:button> is a new tag in JSF 2.0. You can declared the navigation outcome directly in the outcome attribute, no need to call a bean to return an outcome like <h:commandButton>.

But, if browser’s with JavaScript disabled, the navigation will failed, because the “h:button” tag is generate an “onclick” event to handle the navigation via window.location.href.

<h:button value="buton" outcome="login" />          

//HTML output

<input type="button" 
       onclick="window.location.href='/ContextRoot/faces/login.xhtml; return false;" 
       value="buton" />

Source : mkyong. My favorite website where you can find decent examples.

Excellent Blog for JSF : BalusC. A Hero to so many people on this forum:).

SRy
  • 2,901
  • 8
  • 36
  • 57