Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a utility managed bean, to move common code and use from different beans?

 
Robin Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!

In my JSF managed beans, I frequently have the need to access some bean which is already in session, fiddle around with it and then set it back to the session. Could it be possible to create a managed bean which serves just as a utility class where the above and other common functionality could be hosted? Is it the right way or is there any other way to achieve the same?

Something like:
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18272
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand. Regardless of whether a session bean is JSF-managed or managed via regular J2EE code, what are you trying to do?

It sounds like you're removing the bean from the session, modifying it, then returning it to the session. What's the point in removing/adding, when you can just leave the bean in the session and modify it in place?
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A singleton??
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:I don't understand. Regardless of whether a session bean is JSF-managed or managed via regular J2EE code, what are you trying to do?

It sounds like you're removing the bean from the session, modifying it, then returning it to the session. What's the point in removing/adding, when you can just leave the bean in the session and modify it in place?

Fully agree. Just get the reference and alter it. Do you know how collections/maps work? I would go through a decent Java book/tutorial which covers collections/maps. This is fairly trivial.

Back to your actual question: depends on the functional requirement, but in this case I think I would have used managed property injection.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Nortje wrote:A singleton??

With this you make two problems from one problem instead of solve one problem.
 
Robin Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, what I want is to centralize the code to get/set any bean in the session. The code I have shown above is getting repeated in many of beans. I want to avoid that.
 
Robin Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:I don't understand. Regardless of whether a session bean is JSF-managed or managed via regular J2EE code, what are you trying to do?

It sounds like you're removing the bean from the session, modifying it, then returning it to the session. What's the point in removing/adding, when you can just leave the bean in the session and modify it in place?


Tim, I use the setSessionBean method mainly to clear a bean from the session, by passing in null.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, there is a smell in the technical design.

What´s the functional requirment for which you need this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic