0

I have a problem but do not know exactly for what I should look. I do not think I'm the first one has this problem!

It is a Java SE application with JSF & Co, basic frameworks.

The application can be submitted comments to threads. I write a comment and then I open another thread (another Browser-Tab) so the comment is posted in the newly opened thread.

I have a ManagedBean with the attribute "selectedThread". The error results from the fact that the property is replaced by the newer one. How can I fix this problem?

I have several of ideas, but all produce the same problem.

Thank you!


@ManagedBean
@SessionScoped
public class ViewBean {



private Thread selectedThread = new Thread(); //Current opened Threas
private String threadId=""; //ThreadId read out from Database by Id convert to Object
private Comment selectedThreadComment = new Comment(); //Comment to be made

Working/Failure steps:

  1. Open:thread_detail.xhtml?id=10
  2. ThreadId and selected Trip setted
  3. Write a comment (selectedThreadComment setted)
  4. Open:thread_detail.xhtml?id=11
  5. Commit Comment

Comment is understandably persisted for id 11 instead of id 10.

It does not matter which Scope i use. There must be a way to save the Comment according to which site is opened.

I hope now my problem is better-defined!

Taryn
  • 242,637
  • 56
  • 362
  • 405

1 Answers1

0

It sounds like as if the scope of the managed bean is too broad for the data it holds. The symptoms indicate that the managed bean is been placed in the session scope, while the data it holds is specific to a single HTTP request or a single view. A session scoped managed bean instance lives as long as the browser session is established. It is been shared across all requests/views within the same session. Any change initiated by one window/tab would get reflected in another window/tab in the same session.

You should then be placing the bean in the request or the view scope instead if it holds solely request or view scoped data. If you have some data which should surely be kept in the session scope, e.g. the logged-in user, then you should split the current session scoped managed bean out into two managed beans, each in the right scope. The session scoped one is then to be used to hold the data representing the logged-in user and the request/view scoped one is then to be used to hold the data representing the submitted form data and/or the view state. You can use @ManagedProperty to inject the session scoped one into the request/view scoped one.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your answer, but the problem ist still existent. I tested every scope. Still i have the same problem. Somehow i have to tell the ManagedBean which "Thread" I am using and for which thread the "Comment" should be persisted. Is it possible to save an object direktly into a HTTP-Session? – user1385980 Aug 13 '12 at 18:07
  • Perhaps you've declared the property as `static` while it shouldn't be static at all? After all, it's only guessing as long as you don't provide an SSCCE. If you can't for some reason, rather hire a local Java EE architect to spot the problem for you. – BalusC Aug 13 '12 at 18:09