views:

874

answers:

2

I'm quite new in JSF and I'm doing some basic things to get a feeling of how one should work with it. In my one of the projects I have a ManagedBean, SessionScoped like below

@ManagedBean(name="user")
@SessionScoped
public class User implements Serializable
// Having a couple of String properties (with setters and getters).

Now, in one page in a form I have a <h:inputText id="firstName" value="#{user.firstName}" ... /> which I would expect to get a value from the user and put it in my bean's property. The second page just displays the input data, accessing it from the bean. (<h:outputText value="${user.firstName}"/>).

The problem is that if after that I go in a third page (just by typing the URL) and I'm trying to use the same line to display once again the data from the bean, no data gets displayed. I was expecting that while the bean is session scoped it should still be available in the current session.

A: 

This can have several causes which you really need to debug and exclude yourself as you're the only one who has instant access to the environment.

  1. The bean has been recreated.
  2. The bean has been removed.
  3. The session has been invalidated.
  4. The session has been timed out.
  5. There's means of a different session.
  6. There's means of a different context path.

To exclude 1 and 2, put a debug breakpoint in bean's default constructor and/or track the bean in the session map. To exclude 3, 4 and 5, track the value of HttpSession#getId(), eventually in JSP by ${pageContext.session.id}. To exclude 6, just ensure that the page is inside the same context (i.e. in the webcontent of the same project).

BalusC
I've tested a bit the things you suggested and it looks that the session ID is the same throughout my run, so I guess this runs out points 3-6. I also noted that the ctor is called everytime the page gets loaded, so it means it being recreated. Any ideas why this could happen?In my form I tried to keep the things as simple as possible, but hopefully not 'simpler'. The form it's just a simple <h:form> and the submit button is like this <h:commandButton id="view" value="Register user" action="view" />
gabic
The bean is apparently declared request scoped instead of session scoped. Did you declare it in faces-config xml as request scoped bean? This would override any annotations. Eithe remove entire bean declaration from faces-config.xml, or change it to session.
BalusC
I have no faces-config.xml in my application. I thought it's enough to use the annotation @SessionScoped. My bean is not referenced in any other existing config file (sun-web.xml and web.xml)
gabic
It should also be enough. Maybe the bean is recreated programmatically by for example `getSessionMap().put("user", new User())`. Try debugging the call stack in the bean constructor.
BalusC
There is nothing in my code that explicitly sets the session var and from what I could see while checking the call stack there are a lot of "Hidden Source calls" and they are all initiated from a Thread.run call (1st thing on my call stack). I've uploaded my dummy project (NetBeans 6.8) on rapidshare in case someone wants to have a look at it (http://rapidshare.com/files/331252524/SimpleRegisterApp.zip.html) Thanks for the help, BalusC.
gabic
Truly they're all initiated from Thread. Where else? ;) Just check the class/method names in the stack. Check if your code or JSF code hasn't done something. Source code is not so relevant, else you can just add yourself. It's all open source. I did one attempt to download your project, but the fscking rapidshare was lying to me that *"Currently too many users are downloading this file, please try again after X minutes or become a member for low cost!"*. I then hit `Ctrl+W`. I myself by the way use box.net.
BalusC
+2  A: 

Make sure you are using import javax.faces.bean.SessionScoped, instead of javax.enterprise...;

Daniel

related questions