kuba44
kuba44

Reputation: 1878

call method before show page in web browser

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?

Upvotes: 0

Views: 316

Answers (1)

BalusC
BalusC

Reputation: 1108782

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:

Upvotes: 1

Related Questions