-2

In JSF 2.2 whenever I have a line like in a xhtml page: <h:commandButton value="Click Me!" action="whatever" />. Clicking this button causes the browser to "move forward" so that I can click the back button and remain on that xhtml page. That means when I click the commandButton ten times I can go back ten times by pressing the back button of the browser. What can I do to make the button not changing the page? I want to be able to click on such a button and in some cases change the page and in some stay on the current one without making a step in history of the browser.

TrackerSB
  • 317
  • 6
  • 19

1 Answers1

2

Using AJAX would be a possible solution for your problem.

If you use a command button inside a form element, a click sends the form data with a post request to the server.

<h:form>
    <h:inputText id="inputId" value ="#{testController.message}"></h:inputText>
    <h:commandButton value="submit" action="#{testController.reverse}" />
</h:form>

If you use the AJAX element inside a command button, just the data specified by the execute attribute will be send to the server. The response contains only a part of the html file specified by the render attribute. This part will be inserted in the existing html file. By using AJAX not the whole page is refreshed therefore it doesn’t affect the browser history.

<h:form>
    <h:inputText id="inputId" value ="#{testController.message}"></h:inputText>
    <h:commandButton value="inputId" action="#{testController.reverse}" >
        <f:ajax execute="inputId" render="outputId" />
    </h:commandButton>
</h:form>

<h:outputText id="outputId" value="#{testController.output}"/>
Patrick
  • 447
  • 8
  • 17
  • Just putting the commandButton into a form is not enough. That´s already the case in my xhtml. – TrackerSB Sep 05 '16 at 05:41
  • Using `@form` instead of the id does the trick. – TrackerSB Sep 05 '16 at 05:47
  • With the render attribute you can define on or more components that will be refreshed after an Ajax request. If you use @form all components within the form will be updated. – Patrick Sep 11 '16 at 10:26
  • To keep the page responsive, update only the relevant components. Especially try to avoid @all. – Patrick Sep 11 '16 at 10:34