Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser back button doesn't clear old backing bean values

Tags:

jsf-2

We use JSF2, we have a page with form fields with command button linked to backing bean.

When we access the form page, enter the values and submit, the backing bean recieves the correct values and takes it to the next page. If I press back button in the browser, it takes to previous page with form. If I enter new values in the form and submit, the backing bean still recieves the old values in Step 1.

like image 669
user684434 Avatar asked Sep 15 '11 21:09

user684434


1 Answers

This is odd. Perhaps your bean is put in the session scope and/or the browser has requested the page from the cache. To start, you'd like to disable browser cache for all dynamic pages by a filter which sets the proper response headers.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    if (!req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        res.setDateHeader("Expires", 0); // Proxies.
    }

    chain.doFilter(request, response);
}

Map this filter on the servlet name of the FacesServlet or on the same URL pattern.

Last, but not least, do not put your form beans in the session scope. Put them in the request or view scope.

like image 177
BalusC Avatar answered Oct 05 '22 13:10

BalusC