JSF2 PRG and validation error

I have not yet found the final implementation of the PRG template with JSF2.
The BalusC blog presents a very good solution , but as the author claims, such a solution does not apply to JSF2.
Flash-based solutions work fine if a validation error does not occur, but in the event of a validation error, REDIRECT fails because the lifecycle does not call the NavigationHandler. Assuming that the pages are not cached by the browser (setting the correct headers in the HTTP response), if a validation error occurs and the user clicks the "reload" button of the browser, then POST is executed, not GET.
What is the best practice for implementing a robust PRG template that works even with validation errors using JSF2?

+3
source share
1 answer

I faced the same problem. I did not find a 100% satisfactory solution. But look at this approach from BalusC . This solution will not save values ​​if you press the return button of the browser without controlling the browser cache.

<h:form>
  <h:inputText id="somefield" required="true" value="#{formbean.someProperty}"/>
  <h:message for="somefield" />
  <h:commandButton value="POST with redirect and ajax" action="third?faces-redirect=true">
    <f:ajax execute="@form" render="@form" />
  </h:commandButton>
</h:form>

Download the page, leave the field blank, publish the form (-> an error message appears), enter the data, publish it again and click the "Back" button. This will display the form again, the error message has disappeared, but the value of the input field has also disappeared.

, - formbeans ( , flash, view-params,...), , (GET-) "". :

JSF-:

<f:view>
  <f:event type="preRenderView" listener="#{formbean.doNotCache}"/>
</f:view>

Bean:

public void doNotCache(ComponentSystemEvent event) {
  // Using Omnifaces to simplify code
  Faces.getResponse().setHeader("Pragma", "no-cache");
  Faces.getResponse().setHeader("Cache-Control", "no-cache");
  Faces.getResponse().addHeader("Cache-Control", "must-revalidate");
  Faces.getResponse().addHeader("Cache-Control", "no-store");
}

, . , , Post-Redirect-Get-Pattern. ( ) . (, , , / ?). , , , .

0

All Articles