1

I have a simple authorization page which intends the user to type login and password. Based on the results of user input, I define the next page to display. I have a table in db that contains id, user's login and password, and user's role, so the next page will be defined based on information that will be retrieved from the role field of the record which corresponds to inputed login and pass. But also I have a table in db which contains all of the user's info(about 10 fields) of one type of users and another table which contains info about another type of users(4 fields). So my db has such a structure:

Table 1:
  id
  login
  pass
  role

Table 2:
  id(reference to id of table 1)
  other fields....

Table 3:
  id(reference to id of table 1)
  other info....

I think that my app has to have a managed bean(requestScope) for authorization that will take login and password. That bean will have an action controller for submitting authorization form. In that action controller I have to determine the role of user and based on that information create a session scoped managed bean for concrete type of user(app will have separate managed bean for different types of users). Also I have in that action controller to pass specific info for instatiation the session scoped bean for user(id of user). But can I simply instatiate a session scope managed bean(with help of new operator) in action method of request scope managed bean? Also how can I use methods of one session scoped bean in another session scoped bean(Suppose I create 2 session scoped managed beans which are responsible for different services, how can i call method of one in another). Maybe the described method isn't good for such a problem? If so can you give an advise for better design approach?

maks
  • 5,911
  • 17
  • 79
  • 123

1 Answers1

3

Accessing one managed bean from another

To answer this directly, you normally use @ManagedProperty to inject one managed bean in another managed bean. Yes, it will auto-create the beans, but that should be particularly cheap.


But can I simply instatiate a session scope managed bean(with help of new operator) in action method of request scope managed bean?

Yes, that's possible. Just store it in ExternalContext#getSessionMap() with the managed bean name as key.

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getSessionMap().put("bean", new Bean());

It'll be available by #{bean} and so on. Whether that's a good approach is a second.


As to your concrete functional requirement. I think it's much better to have just a single session scoped managed bean with the desired data as properties. You could then for example just check if the User property is not null to determine if the user is logged in. You can find a bean example in this answer: Programmatically control login with Servlet 3.0.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your answer. I have already seen your example, but will it be a good aproach when there is more than on role in the system? In such solution UserManger bean will have to have class variable for every available type of role and only one of this variable will be used at a session. Also such a bean will have respective number of isLoggedIn methods. Another pitfall is that class User may have a lot of fields and if there are many users at one time(each user has its own session) server will not have so much memory. So will it be better to store only user id in session? – maks Aug 26 '11 at 21:44
  • Give `User` a property of `Set`. As to memory concern, let JPA (or whatever you use) lazily fetch the nested properties like `UserDetail` or something. – BalusC Aug 26 '11 at 21:53
  • How about the fact that different users have different properties? E.g. first user may have only name and surname as attributes, another user may have such attributes as preferredLanguage or schoolNumber. Each type of user will have its own pairs of getters/setters. If User class only(with Set property) is responsible for specific info it will have a large amount of getters/setters with a lot of code for determination role in every method – maks Aug 26 '11 at 22:32