1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2219247
  • 1,313
  • 10
  • 20
  • 26

1 Answers1

5

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

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • That's great answers. I am just starting to get JSF working with Spring. I didn't know that I can have a view scope with Spring. The reason I am using ManagedBean with Component is that the Component serves Spring DI purpose and from ManagedBean I can display values in the view. I wouldn't be able to use it in JSF tags if it wouldn't be annotated with ManagedBean. But yeah, this is now clear. Thanks very much! – user2219247 Jun 24 '13 at 15:11