0

I have web application with two pages A and B. From page A I navigate to B side by h:commandButton and make method from backing bean, which return B.xhtml.

When I'm at B page I want to come back to A page by "Back" button in my web browser. And before come back to A page I want to call method from backing bean.

I try do this by

<f:metadata> 
     <f:event type="preRenderView" listener="#{userManager.myMethod}" />
</f:metadata>

but it wasn't work. Do you know any other idea?

kuba44
  • 1,838
  • 9
  • 34
  • 62
  • Can you provide a whole testable [SSCCE](http://sscce.org/) for your case? With your provided info it seems imposible to notice where the problem is. Where are you placing that event? – Aritz Nov 29 '13 at 09:33

1 Answers1

1

It won't "work" when the back button didn't actually hit the server, but instead displayed the previously obtained page from the browser cache. This will then happen without any HTTP request to the server, so any server side code associated with producing the HTML output of the page won't be invoked.

You can solve this by instructing the browser to not cache those pages, so that they will always be requested straight from the server and therefore execute all server side code associated with producing the HTML output of the page, such as preRenderView event listeners.

As a kickoff example, this filter should do it, assuming that your FacesServlet is in web.xml registered on a <servlet-name> of facesServlet:

@WebFilter(servletNames = "facesServlet")
public class NoCacheFilter implements Filter {

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

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

        chain.doFilter(req, res);
    }

    // init() and destroy() can be kept NOOP.
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555