Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating managed bean with different parametrs  RSS feed

 
Maks Besida
Greenhorn
Posts: 13
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm writing a small web app for learning purpose only. My app has a login form which is used for login to different types of users(actually I have 2 types of users and each has different properties). I store info about users in database - first table(id, login, password), second table(id, info about user 1), third table(id, info about user 2). ID fields in second and third tables are foreign keys to ID field in first table. For each type of user I have a transfer object wich is actually a POJO.
I have a request scoped manged bean that handles the input of login and password and has to define on which page to redirect the user. So in action method of that bean I have to define the type of user and also instantiate a session scoped managed bean for specified type of user. Actually I have designed 2 session scoped beans for each type of user. That beans have such fields: id - id of the user, a few dao objects - for processing database access. Also that beans have such methods as (of course each bean has its own UserDTO) for accessing concrete properties of users.
I've found such solution to instantiate a bean inside other bean's action method:

Actually I can create a constructor of MyBean with parametres and pass the id(which was defined from login and password) of the user to it: . But I think it's a bit tricky way, beacause jsf is a pull-based framework and such solution is from push-based(we mannualy put beans into session).
Is there any other method to instantiate session scoped beans with certain parametres without using jsf specific code?(as one's says: good backing beans have no jsf specific code inside)
Or maybe my design approach for this problem is completly wrong? So advise better approach.

Thanks in advance
 
H Jetly
Ranch Hand
Posts: 41
Google Web Toolkit Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using JSF 1.2 or JSF 2.0 ?
Can you please publish your faces-config.xml also.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the technical term for webapps that implement their own security system is "hacked". But that's another matter.

JSF is an Inversion of Control system. IoC means that you don't go out and get things, instead the things are delivered to you. The advantage of this is that you don't have to load down your app objects with framework-specific service/object locator code, but instead can code Plain Old Java Objects (POJOs) and the framework will wire them together. That helps make things more reusable and also facilitates testing.

One of the fundamental premises of working with POJOS is that they should have a no-argument constructor. That way the framework doesn't have to complicate itself with all sorts of contortions to handle specialized constructors. A simple "newInstance()" is all it takes to make a POJO bean.

As I mentioned, it's extremely unsafe to write your own login system, no matter HOW many J2EE books use login forms as examples. But if you do, you can capture the login credentials in the login form's backing bean, use IoC to inject your user profile bean into the login form's backing bean, and have the login form's login action method configure the user profile. All without requiring constructor arguments.
 
Maks Besida
Greenhorn
Posts: 13
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Jetly wrote:Are you using JSF 1.2 or JSF 2.0 ?

I'm using JSF 2.0

Tim Holloway wrote:
As I mentioned, it's extremely unsafe to write your own login system, no matter HOW many J2EE books use login forms as examples. But if you do, you can capture the login credentials in the login form's backing bean, use IoC to inject your user profile bean into the login form's backing bean, and have the login form's login action method configure the user profile. All without requiring constructor arguments.

1.Well, what is better to use instead in jsf 2.0?
2.If I have more than 2 types of users in my system(each of them requires session scoped managed bean), I will have to inject all these beans into login's form backing bean, but in the concrete session it will be used only one of them, so others will stay in the session
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!