0

When i use

<h:button>

or

<h:link>

i can't make any business action in managed bean ? Do I have to use

<h:form>

and

<h:commandButton>

or

<h:commandLink>

with action param or there is another solution ?

kuba44
  • 1,838
  • 9
  • 34
  • 62

2 Answers2

2

Depends on the kind of request you'd like to fire.

If it needs to be a non-idempotent POST request, just use <h:form> with <h:commandXxx action>. Again depending on the concrete functional requirement, you can render the results conditionally in the same view, or send a redirect to the target view afterwards.

If it needs to be an idempotent GET request, use <h:link>/<h:button> and perform the action in @PostConstruct method in the request/view scoped backing bean associated with the target page. If you need to pass parameters, use <f:param> to set them on <h:link>/<h:button> and use <f:viewParam> and <f:event type="preRenderView"> in target view to set and process them in backing bean associated with target view.

All in all, just use the right tool for the job as specified by concrete functional requirement (which you unfortunately didn't tell anything about in your question).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I would like to invalidate session by `FacesContext.getCurrentInstance().getExternalContext().invalidateSession();` so which method shoude be better for this? – kuba44 Sep 06 '13 at 17:14
  • This clearly needs to be a non-idempotent POST request (you do not want to be able to bookmark/share/refresh it, right?). Use a command button/link and send a redirect to the home page afterwards. See also http://stackoverflow.com/questions/5619827/how-to-invalidate-session-in-jsf-2-0/5620582#5620582 – BalusC Sep 06 '13 at 17:24
0

No, you can't do that with h:link or h:button, there are meant to be bookmarkable GET urls. So if you want to perform business action, you need to use either commandButton or commandLink. Note that it's also worth to use h:outputLink for generating bookmarkable links between pages because is much more SEO friendly.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115