This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSF and the fly likes how to passing parameters to managed beans? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "how to passing parameters to managed beans?" Watch "how to passing parameters to managed beans?" New topic
Author

how to passing parameters to managed beans?

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
hi guys I'm really confused
in 2 managed beans
1:users (users.jsp) for authorizing users
2:inbox (inbox.jsp) for showing inbox
now how I can get the current instance of users (users's current instance) from inbox managed beans?
please help

Open source
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Hi - If I'm reading your question correctly you have 2 stages..

1. users where you I assume are requesting login details... then authorizing a user before moving onto to stage

2. where you display the inbox for the logged in user..


If thats the case.. My typical approach to this would be to

a. Create a User object in your session bean.
b. Upon successfull login, set the session bean User variable to that logged in user.
c. From then on when you need the user object, you just access it via the session.
d. You can use this approach to ensure the user is logged in before any call to your inbox page is made... if the user in the session is null.. then redirect to an error page...

Hope that helps clear it up if I understood you correctly...


Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Shasi Mitra
Ranch Hand

Joined: Nov 27, 2008
Posts: 101

Use this code.

FacesContext facesContext = FacesContext.getCurrentInstance();
SecondBean sBean = (SecondBean)facesContext .getApplication().getVariableResolver().resolveVariable(facesContext ,"sBean");

sBean should be the managed bean name in the faces-config.xml.

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Shasi Mitra wrote:Use this code.

FacesContext facesContext = FacesContext.getCurrentInstance();
SecondBean sBean = (SecondBean)facesContext .getApplication().getVariableResolver().resolveVariable(facesContext ,"sBean");

sBean should be the managed bean name in the faces-config.xml.



Wouldnt the properties of this bean only be available if its marked as sessionscope in the faces-config? By the sounds of it he will want to access the 'user' that was authenticated.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
the code you said throws "java.lang.NullPointerException"
okay lets look at it again

1:users should login to the system, it done with users.java managed bean
so if a user don't login and wants open inbox.jsp , system should redirect the page to login.jsp

I just want know how I can get the current instance of a managed bean, from any other managed bean?

I don't know what is it for exactly! but I tried it too, but it didn't work too
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
As I said.. Why not keep your logged in 'User' object in session.

Then you can check for that in any other managed bean that requries a User to be present.

Without intending on sounding arrogant, do you understand the JSF "scope" concept, i.e Request Scope, Session Scope? because if not, then I'm sure as soon as you understand session scope, then all will become very clear.





ArAsh Dex wrote:the code you said throws "java.lang.NullPointerException"
okay lets look at it again

1:users should login to the system, it done with users.java managed bean
so if a user don't login and wants open inbox.jsp , system should redirect the page to login.jsp

I just want know how I can get the current instance of a managed bean, from any other managed bean?

I don't know what is it for exactly! but I tried it too, but it didn't work too
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14480
    
    7

With the exception of the datamodel classes, if your backing bean references JSF classes, there's a strong chance you're doing it "wrong". Meaning non-portable, ugly, and harder to maintain/test.

When a backing bean needs access to another backing bean in JSF, the cleanest, simplest and most reusable way to accomplish this is simply to let JSF itself make the connection.

In other words, provide a setter method in the target bean and code up the faces-config file to inject the source bean into the target bean. You don't need to write any Java code at all.

If you do that, not only will you avoid having to provide those nasty drawn-out chained method calls of JSF-specific code in your target bean, you'll have a bean that can probably be tested and re-used in non-JSF environments.

Inversion of Control (IoC) is one of the fundamental concepts underlying JSF. It's worth understanding.


Customer surveys are for companies who didn't pay proper attention to begin with.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
yes I set my "user" managed bean as session scope

okay I decided, pass my user instance to other guy managed bean via "faces-config.xml" as showing below
but still application throws null exception
please help!

faces-config.xml


and this is my user.java


and bookAdd.java


thanks
Taryn Mervis
Greenhorn

Joined: May 01, 2009
Posts: 2
Try first adding it to a string and then the bean i used the code to to get the variable to pass it to another method as parameter and it worked for me so.

String pass =(String)FacesContext.getCurrentInstance().getApplication().createValueBinding("#{passBean.password}").getValue(FacesContext.getCurrentInstance());

hope it helps
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
no just password attribute
any other solution
adding book or reading mails, requires logging to system
user should logging in to system with grant (admin) access for adding books
and adding book operation should occur in bookAdd class

Mr. Dave Brown, I'm really sorry I'm student and new in JSF, whould you mind giving me an example of your solution?
thanks
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to passing parameters to managed beans?
 
Similar Threads
Search for Entity beans
Face Managed Bean
Face managed bean in RAD7
Class diagram for JSF based application
managed -and backing beans question.