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

查看:117
本文介绍了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.

我的豆子看起来像这样:

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 中添加此配置(来自

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