Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Prevent commandLink to reload page

I need to prevent it because I'm losing the params from the page when the button is clicked.

This is my code:

<h:commandLink styleClass="ui-btn ui-btn-c ui-icon-arrow-r ui-btn-icon-right" value="Continuar" action="#{metaWEB.btnProximo()}" />

I found some results here, like that How make commandButton not fully refresh page? How to use f:ajax?

But my action use messages from the page, so it need reload the form, but the page not.

I found this thread https://stackoverflow.com/questions/18868130/preventing-jsf-commandbutton-from-reloading-the-page/18868374#18868374

I think could be my solution, but I don't understand what he did :(

Thanks Advanced

like image 845
Cechinel Avatar asked Jan 16 '14 13:01

Cechinel


2 Answers

Just make it an ajax (asynchronous) button instead of a regular (synchronous) button. It's a matter of nesting a <f:ajax> inside the existing tag.

<h:commandLink ...>
    <f:ajax execute="@form" render="@form" />
</h:commandLink>

The execute="@form" tells JSF to process the entire form (all input components enclosed in the form the command component is sitting in). The render="@form" tells JSF to update the entire form as ajax response (so if you display messages inside the very same form, they would be updated as well).

Don't forget to make sure that you return null or void from the button's action method, otherwise the view may still change as consequence of a non-null/void navigation outcome.

See also:

  • Communication in JSF 2.0 - Ajax (asynchronous) POST form
like image 95
BalusC Avatar answered Nov 18 '22 06:11

BalusC


Just remember to change the render="@none" to not reload the page...

<f:ajax execute="@form" render="@none" />
like image 3
Sergeenho Avatar answered Nov 18 '22 05:11

Sergeenho