使用浏览器后退按钮时重定向副作用 [英] Redirect side effects when using browser back button

查看:87
本文介绍了使用浏览器后退按钮时重定向副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用:

  • JSF 1.2-1.2_07-b03-FCS
  • JSTL 1_1-mr2(特殊版本)
  • Java 1.6.0_22-b04
  • Eclipse 3.6.0(Helios)
  • Tomcat 6.0.28(需要同时在Weblogic上运行)
  • IE 7.0.5730.13
  • Firefox:6.0

我们有mainForm.jspfilterForm.jspexternalForm.jsp视图.

mainForm.jsp上有一个过滤器"按钮,可转换为filterForm.jsp.它是通过导航规则做到的:

There is a button "Filter" on mainForm.jsp to transition to filterForm.jsp. It does that by a navigation rule:

<h:commandButton value="Filter" action="#{mainBean.filterData}" />

mainBean.java代码为:

public String filterData() {
    doStuff();
    return "filter";
}

这最初可以正确转换为filterForm.jsp.我们可以使用浏览器后退按钮返回mainForm.jsp.我们可以重复执行此操作.

This initially transitions correctly to filterForm.jsp. We can go back to the mainForm.jsp with the browser back button. We can do this repeatedly.

mainForm.jsp上,我们有一个表(实际上是ILog Gantt图表,但我们认为没有关系),在图表栏上带有弹出"菜单.菜单选项之一是重定向到externalForm.jsp.

On mainForm.jsp we have a table (actually an ILog Gantt Chart, but we don't think that matters), with a "popup" menu on the chart bars. One of the menu options is to redirect to the externalForm.jsp.

选择重定向"会触发mainBean.java中的以下方法:

Selecting the "redirect" triggers the following method in mainBean.java:

public void redirect(FacesMenuActionEvent event) {
    if (svr == null) {
        svr = new SetupURL(); // Simple code to set up the full URL
    }

    redirectUrl = svr.redirect(event);  // URL of externalForm.jsp
    svr.redirectData(redirectUrl);
}

这正常工作.窗口切换到externalForm.jsp.

This correctly works. The window transitions to externalForm.jsp.

如果我们按下externalForm.jsp上的浏览器后退按钮,我们将返回到mainForm.jsp,一切看起来还可以.

If we press the browser back button on externalForm.jsp, we transition back to mainForm.jsp and things look OK.

如果我们再按过滤器"按钮,则代码不会执行filterData()方法,而是会执行redirect()方法,然后过渡到externalForm.jsp视图,而不是filterForm.jsp视图.

If we then press the "Filter" button, the code does not execute the filterData() method, but instead it executes the redirect() method and we transition to the externalForm.jsp view, not the filterForm.jsp view.

为什么要这样做,以及如何强制调用filterData()?

Why does it do this and how do we force a call to filterData() instead?

******************************************************************************

BalusC - thank you for your solution.  With a few slight tweeks, it worked great.

The changes I made to web.xml are:

    <filter>
       <filter-name>noCacheFilter</filter-name>
       <filter-class>{my package name}.CacheControlFilter</filter-class>
    </filter>
    <filter-mapping>
       <filter-name>noCacheFilter</filter-name>
       <url-pattern>/faces/*</url-pattern>
    </filter-mapping>




Also, I found another solution:

faces-config.xml add:

    <lifecycle>
        <phase-listener id="nocache">{my package name}.
        CacheControlPhaseListener</phase-listener>
    </lifecycle>    


Create file CacheControlPhaseListener.java:

package {my package name};

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletResponse;

public class CacheControlPhaseListener implements PhaseListener {

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    public void afterPhase(PhaseEvent event) {
    }

    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        HttpServletResponse response = (HttpServletResponse) facesContext
                .getExternalContext().getResponse();
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        // Stronger according to blog comment below that references HTTP spec
        response.addHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "must-revalidate");
        // some date in the past
        response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");
    }
}

Both seem to work.



Thanks,
John

推荐答案

浏览器后退按钮从浏览器缓存中请求结果页面.它不要求服务器重新创建新页面.在您的特定情况下,很可能缓存页面包含当前无效的JSF视图状态.

The browser back button requests the result page from the browser cache. It does not request a fresh new page from the server. In your particular case, most likely the cached page contained a currently invalidated JSF view state.

最好的解决方法是指示浏览器不要缓存动态JSF页面,而是每次都直接从服务器请求它.您可以借助doFilter()方法中包含以下几行的Filter来做到这一点:

Best fix is to instruct the browser to not cache the dynamic JSF pages, but instead requests it straight from the server everytime. You can do that with help of a Filter which has the following lines in the doFilter() method:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    HttpServletResponse res = (HttpServletResponse) response;
    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);
}

将此映射到web.xmlFacesServlet的同一<servlet-name>.例如

Map this on the same <servlet-name> of the FacesServlet in web.xml. E.g.

<filter>
   <filter-name>noCacheFilter</filter-name>
   <filter-class>com.example.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>noCacheFilter</filter-name>
   <servlet-name>facesServlet</servlet-name>
</filter-mapping>

这篇关于使用浏览器后退按钮时重定向副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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