This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSF and the fly likes Bean confusion! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSF
Bookmark "Bean confusion!" Watch "Bean confusion!" New topic

Bean confusion!

Mark Varley

Joined: May 20, 2003
Posts: 15
I've been working with JSF for about a week now working through the very good Core JSF book. I am very confused about the uses for binding and value. Let's follow the example of managing users...

I originally created my pages bound to a managed User bean (scope:session) which I bound to all pages which needed it using value="#{user.myProperty}" syntax. For actions I called the action="#{user.myAction}" via public methods on my User bean for login, logout and signup each of which interrogated a database of users. This was working great. However, got to the section in the book which introduced binding, ActionListeners etc. and am told I should be keeping the User bean, as a business object, separate and instead use Backing Beans to manage login, logout and signup.

Do I therefore create separate LoginBean, LogoutBean and SignUp beans? Does each have to have UIComponent properties for all of the fields on the page and specify binding="#{backingBean.myComponent}" in the page - if so what does the value="" get set to if anything at all? What about the actual user - do I just populate an instance of the user at login and put it in the session? If so should this be a managed bean specified in the faces-config file or do I just add it to the session from my java code?

Apologies if the above is gibberish - I really am quite confused with all this!

Thanks in advance for your help and advice.

Joe Matthew
Ranch Hand

Joined: Jun 10, 2008
Posts: 66
I am a bit confused on what you are trying to convey. But here is a piece of my mind on JSF.

Backing Beans are meant to support the page you are displaying. The UI component in that page will be supported by binding it to members in your backing bean. Consider a BB for a functionality not for a user. BB is in other words a manged bean. But when you retain a BB in session scope, you will end up coupling the user to that BB.

To store user information, you can use a session BB to store the details, but be careful when dealing with session objects. If you dont manage them properly, they have a hit to performance and memory of your application.

I use a session BB to store the information, but I store only 3 or 4 strings at the most. I will be able to get the details from this BB in other BB from the session scope.

On the question of using many BB for user management, I wont be able to give you a specific answer since it depends on a lot of factors like what you will be doing when a user logins, signs up and logouts, what are the other components used in your framework, etc.


Regards,<br />Joe<br /> <br />"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."<br />--Martin Golding
Mark Varley

Joined: May 20, 2003
Posts: 15
Thanks Joe and apologies for the slightly cryptic question - yesterday was a long day and I posted this when I was at the end of my patience!

I have 3 pages which perform specific actions against a User. I also want to be able to access information from the User object from every page to display the "You are logged in as Fred Bloggs. Logout" at the top of the screen.

My pages are login.jsp, signup.jsp, editdetails.jsp. Originally, I had created a managed User bean in the session which in addition to storing all of the details with corresponding getters and setters I also had methods for login(), signup() and edit() which made calls to a database of users. For every page which referenced the User i bound the values using value=#{user.username} etc. This worked fine, although from your response it sounds like I am creating an unnecessary burden on the server.

I think you are suggesting (and please correct me if I am wrong) that I tie my pages to specific backing beans in the request e.g.

login.jsp -> LoginUserBean
signup.jsp -> SignupUserBean
editdetails.jsp -> EditUserDetailsBean

The beans above are bound to my pages using the binding=#{loginUser.username} syntax and are responsible for managing a simple User bean which goes in the session. If this is the case do I need to declare User as a managed bean in the faces-config file?

Please advise whether the above i correct and if not what you suggest and I will adopt the same principles, not just for user handling, but throughout my application.

Thanks in advance,

Joe Matthew
Ranch Hand

Joined: Jun 10, 2008
Posts: 66
I do think you are following the right approach. Have request BB to support pages are correct. But if three BB are very small, it wouldnt make much sense to maintain three classes. You can use a single BB. Just a suggestion.

But the approach you have is good. You can have these request BB update the session bean you need to maintain.

Having very small amount of data in session is fine, if your memory is not too small.

Yes, you will need to specify the managed bean in the faces-config.xml. Not needed if you want use a session POJO bean.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Howzit Mark,

Firstly, before I say anything, whatever I mention below I do stand to be corrected, 'cos my theory is not up to scratch, so this is just what I think.

I haven't read the core JSF book, but what I think they're trying to tell you is to have managed beans map one to one with your use cases. Like LoginBean. That's not necessarily one per page. And and POJO's the represent your business objects. Like User/Person/Whatever.

With regards to your question about binding and value, let's look at what your bean will look like in the following 2 instances.

So that value attribute pulls the value of username from the loginBean

Here you can see that the jsf page pulls the entire component in from the bean. In the bean then you could be setting a value on that component at any point by calling the setValue method.

So in a nutshell, "value" renders the value of a component on your bean and some jsf components know how to render different object, like dataTable's value knows how to render a java.util.List.
And "binding" makes this component that you're bining the same component that you have in your.

This is at least the way I understand it, and use jsf for that matter.

Hope it helps.

Mark Varley

Joined: May 20, 2003
Posts: 15
Thanks for all your help guys - am all sorted now ) mark
Have you checked out Aspose?
subject: Bean confusion!
It's not a secret anymore!