@RequestScoped bean中的数据在不同的浏览器中共享 [英] Data from @RequestScoped bean is shared in different browsers

查看:576
本文介绍了@RequestScoped bean中的数据在不同的浏览器中共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有List属性的@RequestScoped bean.

I have a @RequestScoped bean with a List property.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.springframework.stereotype.Controller;

@Controller
@ManagedBean
@RequestScoped
public class MyBean implements Serializable {

    private List list;

    // getters and setters
}

此属性链接到数据表:

<ice:dataTable value="#{myBean.list}" ..other stuff.. />

列表将动态填充,没有问题,并且数据表显示没有问题.但是,如果我导航到另一个页面,然后返回到初始页面,则数据表仍然包含初始请求的数据.它不应该再为空吗?如果bean是请求范围的,则应在请求之后销毁它,并且我应该以空数据表作为开始.

The List is dynamically filled with no problem, and the datatable is displayed with no problems. But if, I navigate to another page, and then go back to the initial page the datatable is still with the data of the initial request. It shouldn't be empty again? If the bean is request scoped it should be destroyed after the request, and I should get and empty datatable as the beginning.

更奇怪的是,如果我在一个浏览器(如Firefox)中打开页面,用请求填充数据表,然后我打开另一个浏览器(如Chrome)并转到数据表页面,则该页面中填充了数据来自另一个浏览器的先前请求!我认为bean的行为就像是一个应用程序.

Even more strange is that if I open the page in one browser (like Firefox), fill the datatable with a request, then I open another browser (like Chrome) and go to the datatable page, it is filled with the data from previous request from another browser! I think the bean is behaving like an application one.

有什么想法吗?

更新1 :该类不是静态的,也不是其变量.另外,我禁用了tomcat缓存,但仍然无法正常工作.

Update 1: The class is not static neither its variables. Also, I disable tomcat cache, but still not working.

更新2 :我认为可能是找到了问题.我的后备豆在Spring中用@Controller进行了注释.我使用此批注,因为然后使用@Autowired绑定服务.可能这是在创建一个单例,为什么没有在每个请求中创建和销毁它?我认为可以肯定,问题出在Spring和JSF2批注的混合使用中.

Update 2: I think probably found the problem. My backing beans are annotated with @Controller from Spring. I use this annotation because then use @Autowired to bind services. Could be this is creating a singleton and that why is not being created and destroyed with every request? I think pretty sure the problem is in the mix of Spring and JSF2 annotations.

推荐答案

您不应通过多个不同的bean管理框架(例如JSF,CDI和Spring)来管理单个bean.选择一个或另一个.例如,当通过Spring的@Controller管理Bean时,其他框架(如JSF的@ManagedBean和CDI的@Named)的所有与Bean管理相关的注释将被忽略.

You shouldn't manage a single bean by multiple different bean management frameworks like JSF, CDI and Spring. Choose the one or the other. When managing the bean by for example Spring's @Controller, all bean management related annotations of other frameworks like JSF's @ManagedBean and CDI's @Named are ignored.

我不使用Spring,也不知道为什么要使用它而不是标准的Java EE 6 API,但是症状和文档表明这种Spring bean的范围确实是应用程序范围的默认值.您需要通过Spring @Scope批注指定bean范围.您还希望删除JSF bean管理注释,因为它们不再具有任何价值,只会使开发人员/维护人员感到困惑.

I don't do Spring and I have no idea why you're using it instead of the standard Java EE 6 API, but the symptoms and documentation indicates that the scope of such a Spring bean indeed defaults to the application scope. You need to specify the bean scope by Spring @Scope annotation. You would also like to remove the JSF bean management annotations since they have no value anymore anyway and would only confuse the developer/maintainer.

@Controller
@Scope("request")
public class MyBean implements Serializable {
    // ...
}

或者,您也可以摆脱Spring @Controller批注,并坚持使用JSF @ManagedBean.您可以使用@ManagedProperty而不是@Autowired来注入另一个@ManagedBean实例,甚至可以使用Spring托管bean(如果已配置了Spring Faces EL解析器),或者可以使用Java EE标准@EJB来注入@Stateless@Stateful实例.

Alternatively, you can also get rid of Spring @Controller annotation and stick to JSF @ManagedBean. You can use @ManagedProperty instead of @Autowired to inject another @ManagedBean instance or even a Spring managed bean (if you have Spring Faces EL resolver configured), or the Java EE standard @EJB to inject an @Stateless or @Stateful instance.

例如

@ManagedBean
@RequestScoped
public class MyBean implements Serializable {

    @EJB
    private SomeService service;

    // ...
}

另请参见:

  • 春季JSF集成:如何在JSF托管bean中注入Spring组件/服务?
  • See also:

    • Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
    • 这篇关于@RequestScoped bean中的数据在不同的浏览器中共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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