浏览器后退按钮不会清除旧的后备Bean值 [英] Browser back button doesn't clear old backing bean values

查看:97
本文介绍了浏览器后退按钮不会清除旧的后备Bean值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用JSF2,我们有一个页面,其中的表单字段带有链接到支持bean的命令按钮.

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

当我们访问表单页面时,输入值并提交,后备bean会收到正确的值并将其带到下一页.如果我在浏览器中按后退"按钮,它将进入带有表单的上一页.如果我在表单中输入新值并提交,则支持bean在步骤1中仍会接收旧值.

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.

推荐答案

这很奇怪.也许您的bean被放置在会话范围内和/或浏览器已从缓存请求页面.首先,您想通过过滤器禁用所有动态页面的浏览器缓存,该过滤器会设置适当的响应头

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);
}

将此过滤器映射到FacesServlet的servlet名称或相同的URL模式.

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

最后但并非最不重要的一点是,不要将表单bean放在会话范围内.将它们放入请求或视图范围.

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

这篇关于浏览器后退按钮不会清除旧的后备Bean值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆