Question

In one of our applications we have two classes of users,

  1. Internal Users (class InternalUser extends User)
  2. External Users (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?

Was it helpful?

Solution

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.

OTHER TIPS

Perhaps a managed bean named CurrentUser with an Internal/ExternalUser as a member? In my own application, I am not sure I'd inject this required data by making the User sub-classes double as business objects and managed beans the way you are attempting to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top