By Valter Silva


2012-01-24 14:46:07 8 Comments

I have a button to submit a form and invoke a managed bean action.

<h:form>
    ...
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

But when I press the button it refreshes the whole page and sometimes also changes the URL.

Is there some way to not refresh the page and still invoke the action?

1 comments

@BalusC 2012-01-24 14:49:29

Make use of Ajax. It's a matter of nesting <f:ajax> inside the command button of interest.

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

Particularly the render="@none" (which is the default value anyway, you could just omit the attribute altogether) will instruct JSF to re-render just nothing after the submit. If you intend to re-render only a specific component instead of the whole page, then you could also specify the ID of that specific component in render attribute.

<h:form>
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="result" />
    </h:commandButton>
    ...
    <h:panelGroup id="result">...</h:panelGroup>
</h:form>

If you're already using PrimeFaces, then it's a matter of simply using <p:commandButton> instead of <h:commandButton>. It uses by default ajax already, so you don't need to nest in a <f:ajax>. You only need to remember to use attribute names process and update instead of execute and render.

<h:form>
    ...
    <p:commandButton ... update="result" />
    ...
    <h:panelGroup id="result">...</h:panelGroup>
</h:form>

The execute attribute defaults to @form already, so it could be omitted.

See also:

@eskalera 2014-05-12 17:34:38

Thanks @BalusC. I thought <h:commandButton ajax="true"> was default though. But it doesnt' work without ajax tag. Any idea why?

@BalusC 2014-05-12 17:41:56

@eskalera: you're confusing standard JSF with PrimeFaces.

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How can I refresh a page with jQuery?

4 Answered Questions

[SOLVED] How to refresh a page after download

1 Answered Questions

1 Answered Questions

3 Answered Questions

JSF Issue with Refreshing a page

  • 2014-03-25 22:15:44
  • Sam
  • 2707 View
  • 0 Score
  • 3 Answer
  • Tags:   jsf refresh

1 Answered Questions

[SOLVED] Pass value fetched with page, to managed bean on commandButton press

  • 2012-10-21 14:20:41
  • Rajat Gupta
  • 512 View
  • 0 Score
  • 1 Answer
  • Tags:   jsf

1 Answered Questions

JSF: Unwanted page refresh on action call

  • 2011-05-20 12:06:07
  • geeehhdaa
  • 2335 View
  • 1 Score
  • 1 Answer
  • Tags:   jsf refresh

Sponsored Content