aspose file tools*
The moose likes JSF and the fly likes ViewScoped bean using ManagedProperty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "ViewScoped bean using ManagedProperty" Watch "ViewScoped bean using ManagedProperty" New topic
Author

ViewScoped bean using ManagedProperty

Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
I have a managed property inside a view scoped managed bean like this:where the LoginUser is a session scoped managed bean.

But the loginUser is always null! Why?

When I use the same syntax with request scoped or session scoped beans JSF injects automatically the loginUser where I want. So for example this does work: In addition I know that the loginUser is not actually null since I can access it from the ViewScoped bean with this codeI do not see where the problem is. ViewScoped is considered to be between RequestScoped and SessionScoped so it should not be a "visibility" problem (I mean that for example SessionScoped managed bean can not use RequestScoped managed property since the property is changed in every request).

I'm using MyFaces 2.0.1 so maybe there is a bug in MyFaces? Any thoughts?
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
Hi, is #{loginUser} a pre-defined FacesContext property in MyFaces? it's not in Mojarra as
far as I know. Anyway, I had inconsistency problems when using @ManagedProperty in
Mojarra and reverted to the long hand (for example):

FacesContext.getCurrentInstance().getExternalContext().getLoginUser()

Whilst a bit out of my depth I thought I'd make the suggestion. Good luck with it.

Regards,
Brendan.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
#{loginUser} is a simple managed bean (related to custom login procedure) as I stated above. But when I did some rechecks on this problem now I realized that the actual problem is not only the @ViewScoped bean.
When I check the presence of loginUser normally then it is present no matter if the calling bean is @RequestScoped, @SessionScoped or @ViewScoped. But when I use AJAX call (with f:ajax) then the loginUser is not present if the calling bean is @ViewScoped. This looks like a bug in the implementation of @ViewScoped. And the bug may be only present in the implementation that I use (MyFaces 2.0.1 and 2.0.2).

I may check if the behavior is changed when using the Mojarra implementation...

EDIT: I tested it and for some reason I can not get Mojarra implementation (2.0.1) do the ajax call at all. I do not have time to find what the reason is so this problem remains unresolved.

EDIT2: Argh, tested this with Mojarra 2.0.3 and the ajax call worked but the loginUser remains as null (although it is in the session). So this may be intended for some reason. Can not understand what the reason might be but nevertheless...
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Ilari Moilanen wrote:#{loginUser} is a simple managed bean (related to custom login procedure) as I stated above. But when I did some rechecks on this problem now I realized that the actual problem is not only the @ViewScoped bean.
When I check the presence of loginUser normally then it is present no matter if the calling bean is @RequestScoped, @SessionScoped or @ViewScoped. But when I use AJAX call (with f:ajax) then the loginUser is not present if the calling bean is @ViewScoped. This looks like a bug in the implementation of @ViewScoped. And the bug may be only present in the implementation that I use (MyFaces 2.0.1 and 2.0.2).

I may check if the behavior is changed when using the Mojarra implementation...

EDIT: I tested it and for some reason I can not get Mojarra implementation (2.0.1) do the ajax call at all. I do not have time to find what the reason is so this problem remains unresolved.

EDIT2: Argh, tested this with Mojarra 2.0.3 and the ajax call worked but the loginUser remains as null (although it is in the session). So this may be intended for some reason. Can not understand what the reason might be but nevertheless...


Did you find the solution? I have exactly the same problem.. @ManagedProperty is working fine, ajax doesn't (object is null)
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Alas, I did not find a solution to this problem.

My workaroud is as follows
So I put the check to the normal getter. I just have to remember to use the getter and never the object directly (since the first time it is null).
 
jQuery in Action, 2nd edition
 
subject: ViewScoped bean using ManagedProperty