Sunday 21 February 2016

Handling Browser F5 Refresh in JSF

Problem
If a Web Page had a POST action submitted last and a User clicks the Browser F5 button or initiates a Browser refresh action, it cause the last action to be re-submitted, it could be a problem in your application as it would re-submit the previous POST request to the server. 
Solution
While there could be many solution to handle it at various levels, in this post I am trying to capture various methods we can use while using JSF as Frontend UI technology. Each method is suitable for a specific use-case:

  1. Method-1
    <navigation-rule>
    <from-view-id>/views/submitaction.xhtml</from-view-id>
    <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/views/successConfirmation.xhtml</to-view-id>
    <redirect/>
    </navigation-case>
    </navigation-rule>
  2. Method-2
    <h:commandButton value="send" action="done?faces-redirect=true" />

  3. Method-3
    If the requirement is to move from the original page to the same page after form submission. Ex.
    //In the Baking Bean have this code to return the String outcome:
    String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
    return viewId + "?faces-redirect=true";

  4. Method-4  (if you want objects to survive redirection)
    For JSF1.2: By implementing PhaseListener:


Thanks!

4 comments: