문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top