0

in a jsf 2.2 application, there is page called test.xhtml that take a paramater called ‘id’ , for example test.xhtml?id=200 . The page is backed by a CDI Session bean named ‘TestBean’. The page has this code to load data:

<f:metadata> <f:viewAction action="#{testBean.redirectNoParameters}"></f:viewAction> </f:metadata> 

Now based on the id, the application load a set of fields in the session bean with the right values.

public String redirectNoParameters() { //Code… //Load fields test = testDao.find(id); //Code… } 

Till now is all good.

Except that when the user opens a new tab in the browser and specify a DIFFERENT id, for example test.xhtml?id=300 . the Session bean override the current values of the previous parameter 200, with the new id 300 values.

So my question is how can I use a session bean and deal with many tabs with different parameters? How can I have a session bean for each tab? If this is not possible than what solution do people use for this kind of scenario ? Thanks.

1

2 Answers 2

1

A @SessionScope Bean is living as long as the users Session is active. (hence the name) - It is shared between Requests and Views, which is the "Problem" you are facing.

A @RequestScope Bean will be recreated upon every request (No Matter if first-Access or Ajax-Request), which is possible to use for your usecase (reloading data based on resubmitted IDs), but can be optimized. (This would be a traditional Request/Response-Model as known from PHP - JSF offers a better option)

Your case perfectly matches the @ViewScope. One Bean per View, living as long as the View is present. This would allow you to open an (almost) infinite amount of different Views (hence, the name), each having it's individual set of BackingBeans as long as they are @ViewScope. Multiple "Views" of the same page are possible, each View will maintain the reference to it's dedicated View-Scoped-Beans. (In your example: 2 opened text.xhtml pages and therefore 2 active instances of TestBean, each having its own ID)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try to change the scope.
0

The behaviour of your application is correct. Please read the section about Scopes of The Java EE 6 Tutorial.

Your application has created a session scoped bean for the current user and it will be used by all request for such user. Any variable created in such bean will be shared by any tab the user opens.

If you expect the user to interact on multiple tabs with different values for the same variable, consider using a request scoped bean.

3 Comments

say for example that when you open 'test.xhtml?id=200' , and on the page there is a delete button that remove the entity test whose is is 200. in a session scope i just call testDao.remove(test.getId) where test is a loaded field in the SESSION bean testBean. in a RequestScoped bean, there is NO test entity, fields don't get saved. so How should save 'test' entity for subsequent requests ??!! Thanks
You can pass the ID and action in the url: test.xhtml?id=200&action=delete
deltaspike.apache.org/documentation/jsf.html ViewAccessScoped or WindowScoped

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.