Sunday, 21 February 2016

Handling Browser F5 Refresh in JSF

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. 
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
  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: