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

查看:163
本文介绍了通过链接或后退按钮打开时,强制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/*是您要关闭浏览器缓存的示例网址格式.您可以根据需要将其映射到/**.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 ,则可以使用其内置的

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天全站免登陆