通过链接或后退按钮打开时强制 JSF 刷新页面/视图/表单 [英] Force JSF to refresh page / view / form when opened via link or back button

查看:16
本文介绍了通过链接或后退按钮打开时强制 JSF 刷新页面/视图/表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将数据发布到外部页面的 JSF 页面.数据从 JSF 托管 bean 加载,该 bean 在发布数据中生成唯一 ID.

I have a JSF page which posts data to an external page. The data is loaded from a JSF managed bean which generates a unique ID in the post data.

我遇到了一个问题,用户单击结帐按钮,然后导航回同一页面并再次按下结帐按钮.帖子数据没有更新.此外,根本不调用 bean.有没有办法强制 JSF 重新加载页面和表单数据?

I have an issue where a user clicks on a checkout button then navigates back to the same page and presses the checkout button again. The post data has not updated. Moreover, the bean is not invoked at all. Is there anyway to force JSF to reload the page and the form data?

<form action="#{checkoutBean.externalUrl}" method="post"
    id="payForm" name="payForm">
       <input type="hidden" value="#{checkoutBean.uniqueID}" />
       <input type="submit" value="Proceed to Checkout" />
</form>

推荐答案

该页面可能是从浏览器缓存中加载的.这本质上是无害的,但确实让最终用户感到困惑,因为他/他错误地认为它确实来自服务器.您可以通过查看浏览器 Web 开发人员工具集中的 HTTP 流量监视器轻松确认这一点(在 Chrome/FireFox23+/IE9+ 中按 F12 并选中网络"部分).

That page is likely being loaded from browser cache. This is essentially harmless, but indeed confusing to the enduser, because s/he incorrectly thinks that it's really coming from the server. You can easily confirm this by looking at the HTTP traffic monitor in browser's web developer toolset (press F12 in Chrome/FireFox23+/IE9+ and check "Network" section).

您基本上需要告诉浏览器不要缓存(动态)JSF 页面.通过这种方式,浏览器实际上会向服务器请求页面(并由此触发托管 bean 的正确创建/初始化等),而不是从其缓存中显示先前请求的页面.

You basically need to tell the browser to not cache (dynamic) JSF pages. This way the browser will actually request the server for the page (and hereby triggering proper creation/initialization of managed beans and so forth) instead of showing the previously requested one from its cache.

通常,这是通过一个简单的servlet过滤器来完成的,如下所示:

Generally, this is to be done with a simple servlet filter like follows:

@WebFilter("/app/*")
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);
    }

    // ...
}

其中 /app/* 是您要关闭浏览器缓存的示例 URL 模式.如有必要,您可以将其映射到 /**.xhtml 甚至 servletNames={"Faces Servlet"}.

Where /app/* is the example URL pattern on which you'd like to turn off the browser cache. You can if necessary map it on /*, *.xhtml or even on servletNames={"Faces Servlet"}.

如果您碰巧使用 JSF 实用程序库 OmniFaces,那么您可以使用其内置的 CacheControlFilter 只需将以下条目添加到 web.xml(其中演示了对 FacesServlet 的直接映射,这意味着不会缓存每个动态 JSF 页面):

If you happen to use JSF utility library OmniFaces, then you can use its builtin CacheControlFilter by just adding the following entry to web.xml (which demonstrates a direct mapping on FacesServlet, meaning that every dynamic JSF page won't be cached):

<filter>
    <filter-name>noCache</filter-name>
    <filter-class>org.omnifaces.filter.CacheControlFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>noCache</filter-name>
    <servlet-name>facesServlet</servlet-name>
</filter-mapping>

另见展示.

这篇关于通过链接或后退按钮打开时强制 JSF 刷新页面/视图/表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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