• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creating managed bean with different parametrs

 
Greenhorn
Posts: 13
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
Google Web Toolkit Tomcat Server Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using JSF 1.2 or JSF 2.0 ?
Can you please publish your faces-config.xml also.
 
Saloon Keeper
Posts: 25477
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic