user684434
user684434

Reputation: 1165

Browser back button doesn't clear old backing bean values

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.

Upvotes: 5

Views: 5034

Answers (2)

Armen
Armen

Reputation: 21

learn JSF carefully, JSF2.0 in the glassfish work nice, if you have viewscoped beans and press browser back/next links JSF carefully loaded the pervious page also you can see your data and submit it.

Upvotes: 0

BalusC
BalusC

Reputation: 1108722

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.

Upvotes: 6

Related Questions