浏览器返回 + viewscope bean [英] browser back + viewscope beans

查看:22
本文介绍了浏览器返回 + viewscope bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是什么:当单击浏览器后退按钮时会发生什么情况 --> 打开一个其 viewscoped-managedbean 已经被销毁的页面 --> 使用 grid-record-selections 提交来自该页面的 commandButton 的请求?

What the problem is : What happens when clicking on the browser back button --> opens up a page whose viewscoped-managedbean is already destroyed --> submit a request from a commandButton from that page with grid-record-selections ?

我的期望:关联的 viewscope-managebean 被重新创建,接收网格记录选择,并处理它们,就好像浏览器的后退按钮从未涉及一样.

What i expect : The associated viewscope-managebean is re-created, receives the grid-record-selections, and deal with them as if the browser back button is never involved.

我的经历:关联的 viewscope-managebean 不会重新创建,也不会接收网格记录选择.必须重新输入 URL,或在单击浏览器后退按钮后按 F5 才能再次正常工作.

What i experience : The associated viewscope-managebean is NOT re-created, doesnt receive the grid-record-selections. Have to reenter the URL, or F5 after clicking on the browser-back button for it to work properly again.

所以这里是成功场景,所有 bean 都是 viewscoped bean :

So here's the success scenario, all beans are viewscoped beans :

  1. GET page1.xhtml -->在@PostConstruct中创建page1Bean,查询数据等
  2. 从数据表中检查/选择多条记录,点击处理按钮
  3. page1Bean 的处理方法将选中的记录存储在 flash 对象中,并重定向到 page2.xhtml
  4. page1Bean销毁,page2Bean创建,在preRenderView监听器方法中,从flash对象中取出选中的记录,并进行处理
  5. 点击go to main page"命令按钮重定向到page1.xhtml,page2Bean销毁,page1Bean重新创建
  6. 从 2 到 5 的循环仍然可行

现在,这是涉及浏览器后退按钮的错误场景(从 #6 开始发生不同的事情):

Now, this is the errornous scenario involving the browser back button (different stuffs happening starting from #6) :

  1. GET page1.xhtml -->在@PostConstruct中创建page1Bean,查询数据等
  2. 从数据表中检查/选择多条记录,点击处理按钮
  3. page1Bean 的处理方法将选中的记录存储在 flash 对象中,并重定向到 page2.xhtml
  4. page1Bean销毁,page2Bean创建,在preRenderView监听器方法中,从flash对象中取出选中的记录,并进行处理
  5. 点击浏览器后退按钮page2Bean没有被销毁,page1Bean没有被创建
  6. 从数据表中检查/选择多条记录,点击处理按钮
  7. page1Bean 方法执行了(奇怪,因为 page1Bean 应该已经被销毁了),但是看不到所做的记录选择,并重定向到 page2.xhtml
  8. page1Bean 没有被销毁(没有日志输出),page2Bean 没有被创建(因为它没有被销毁),像往常一样执行 preRenderView 监听器,但是这次,在 flash 对象中没有选择的记录

使用带有浏览器后退按钮的 viewscope-beans 是否有可能获得正常体验(就像没有浏览器后退按钮一样)?

Is it possible to have the normal experience (as if without the browser back button) with viewscope-beans with the browser back button ?

这是我的依赖:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.faces</artifactId>
    <version>2.1.3</version>
    <scope>compile</scope>
</dependency>

请分享您的想法!

推荐答案

当您将 JSF 状态保存方法设置为 server(这是默认设置).

The browser seems to have served the page from its cache instead of sending a fullworthy HTTP GET request to the server, while you have JSF state saving method set to server (which is the default).

有两种方法可以解决这个问题:

There are 2 ways to solve this problem:

  1. 告诉浏览器不要缓存动态 JSF 页面.您可以在过滤器的帮助下做到这一点.

@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 或其相同的 URL 模式.

Map the filter on the FacesServlet or its same URL-pattern.

将JSF状态保存方式设置为客户端,这样整个视图状态就存储在表单的一个隐藏字段中,而不是在服务器端的会话中.

Set the JSF state saving method to client, so that the entire view state is stored in a hidden field of the form instead of in the session in the server side.

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

过滤方式更可取.

这篇关于浏览器返回 + viewscope bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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