Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

spring - Bean properties are shared across different sessions

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...