In one of our applications we have two classes of users,
class InternalUser extends User
)class ExternalUser extends User
)We are using these as session-scoped managed beans (basically to inject the logged-in user's details, which has some common detail in the class User
and a few specific details in each of the 2 classes stated above).
Can I have the same name for both the managed beans (here "loggedInUser
")?
Faces is throwing an exception "Managed bean named 'loggedInUser' has already been registered. Replacing existing managed bean class..."
How can we manage this scenario?
One way is to just not make it a JSF managed bean. Make the bean associated with the login form a request/view scoped one and put the User
instance in the session scope youself.
E.g.
User user = service.find(username, password);
if (user != null) {
externalContext.getSessionMap().put("user", user);
}
It'll be available as #{user}
in EL scope (and thus also be injectable via @ManagedProperty
in other beans on exactly that expression). In the find()
method, you can just return either InternalUser
or ExternalUser
accordingly.