Bean 属性在不同的会话之间共享 [英] Bean properties are shared across different sessions

查看:18
本文介绍了Bean 属性在不同的会话之间共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用 JSF Mojarra 2.1.13、PrimeFaces 3.5 和 Spring 3.2.3.对于 DI,我使用的是 Spring 方法(不是 CDI).我正在跟随收集器的 PrimeFaces 演示教程:http://www.primefaces.org/showcase/ui/collector.jsf

I am using JSF Mojarra 2.1.13, PrimeFaces 3.5 and Spring 3.2.3 for my application. For DI I am using Spring approach (not CDI). I am following the tutorial on PrimeFaces demos with the collector: http://www.primefaces.org/showcase/ui/collector.jsf

一切正常,我可以将我的值添加到列表中,获取它们等.问题是例如如果我打开两个浏览器并将一些值添加到列表中,那么在另一个浏览器中我也会添加一些值,如果我刷新浏览器,我会看到在两个浏览器中输入的所有值.因此,如果我在一个浏览器中输入两个值,在另一个浏览器中输入两个值,刷新它们后,我会在两个浏览器中看到四个值.我不希望我的价值观在不同的会议上共享.

Everything is working fine, I can add my values to the list, get them, etc.. The problem is that for e.g. if I open two browsers and adding some values into the list, then in another browser I am adding a few values as well, and if I refresh browsers I am seeing the all the values which has been entered in both browsers. So if I enter two values in one browser two in the other, after refreshing them I am seeing four values in both browsers. I want my values to not be shared across different sessions.

我的 bean 看起来像这样:

My bean looks like this:

@Component
@ManagedBean
public class ClientBean extends BaseBean {

    private Client client = new Client();

    private List<Client> clients = new LinkedList<>();

    public String reInit() {
        client = new Client();
        return null;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public List<Client> getClients() {
        return clients;
    }

    public void setClients(List<Client> clients) {
        this.clients = clients;
    }
}

我知道我正在创建全局变量:

I know that I am creating global variables:

private Client client = new Client();    
private List<Client> clients = new LinkedList<>();

但这在教程中显示.那么我该如何处理这种情况以使收集器正常工作,以便这些变量不会在不同会话之间共享?

But this is showed in tutorial. So how can I handle this situation to make collector work so that those variables won't be shared across different sessions?

编辑我试图用:@RequestScoped@SessionScoped 注释我的 bean - 没有用.同样的问题仍然存在.

EDIT I have tried to annotate my bean with: @RequestScoped or @SessionScoped - didn't work. The same problem remains.

推荐答案

不确定你为什么将 @ManagedBean 配置为 @Component 开始.这个问题是因为 Spring 在您的应用程序中处理 @Component 的单个实例(或者至少从您的解释来看是这样的).删除它并在您的托管 bean 中使用 @ViewScoped 以使其按预期工作.请注意,如果您使用 Spring 来管理您的 JSF 托管 bean,那么您必须将此配置添加到您的 faces-config.xml(来自 mkyong 教程):

Not really sure why you configured the @ManagedBean as a @Component to begin with. This problem is because Spring handles a single instance of @Component across your application (or at least that's how it looks from your explanation). Remove it and use @ViewScoped in your managed bean to make this work as expected. Note that if you use Spring to manage your JSF managed beans, then you have to add this configuration in your faces-config.xml (from mkyong tutorial):

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>

但是这样做您将失去 @ViewScoped 托管 bean 的功能.要解决此错误,您必须在 Spring 中实现 @ViewScoped.网上有很多关于这个的例子,看起来最流行的是 卡加泰的

But doing this you will loss the power of @ViewScoped managed beans. To solve this error, you have to implement the @ViewScoped in Spring. There are plenty examples on the net about this, and looks that the most popular is from Cagatay's

有关 JSF 托管 bean 范围的更多信息:通信JSF 2:托管 bean 范围

More info about JSF managed bean scopes: Communication in JSF 2: Managed bean scopes

这篇关于Bean 属性在不同的会话之间共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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