English 中文(简体)
@Managed 认证数据的财产标
原标题:@ManagedProperty object for authentication data is null

我有以下经过集装箱认证后储存标识数据的经过管理的星号:

@ManagedBean(name = "authenticatedUserController")
@SessionScoped
public class AuthenticatedUserController implements Serializable {

@EJB
private jpa.UtentiportaleFacade ejbFacade;

  public Utentiportale getAuthenticatedUser() {
    if (AuthenticatedUser == null) {
        Principal principal =   FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if (principal != null) {
            AuthenticatedUser = ejbFacade.findByLogin(principal.getName()).get(0);
        }
    }
    return AuthenticatedUser;
}

getAuthenticatedUser is called in every page because I put the user name in a facelets template on the top right side. In PermessimerceController, another managedbean, I need to access login data so it is easy and fast to inject the above session scoped controller:

@ManagedProperty(value = "#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

I experienced the following problem: trying to access the page which is linked to PermessimerceController without being authenticated I m redirected to the login page (and this is OK) but after that I get a null pointer exception because authenticatedUserController is null when it is injected inside PermessimerceController. The page in question uses both PermessimerceController and AuthenticatedUserController so I should guess that for some reason PermessimerceController is created before AuthenticatedUserController. Can you suggest a simple way to solve this problem ? Alternatively how can I store the login data in an easy to access place ?

Thanks Filippo

I try to edit this post in the hope to clarify better the problem I have and find useful answers. Using facelets templating I show the user login name throught a property of AuthenticatedUserController. The rest of the content is linked to PermessimerceController which needs some informations about the user for filtering data. The @ManagedBean annotation is an easy way to accomplish this. Unfortunately if the user access that page without being authenticated the injected AuthenticatedUserController is null. So it seems PermessimerceController is created before AuthenticatedUserController and I wonder why. Is there a trick I can use for being sure AuthenticatedUserController is create before ?

问题回答

你们显然在神父的构造中去了:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

public PermessimerceController() {
    authenticatedUserController.getAuthenticatedUser(); // Fail!
}

这确实不会奏效。 床铺设 是否注入了依赖物(对此进行思考;依赖性注射管理者如何注入?)

最早的接入点是@PostConstruct。 方法:

@ManagedProperty("#{authenticatedUserController}")
private AuthenticatedUserController authenticatedUserController;

@PostConstruct
public void init() {
    authenticatedUserController.getAuthenticatedUser(); // Success!
}




相关问题
How to reuse fieldlength in form, validation and ddl?

I m working on an Spring application with lots of input forms. I d like to reuse the field length in the UI-form, validation and JPA annotations. Is there an elegant way to solve this. My solution at ...

MVC Toolkit and XmlMetadataProvider

I have been working on validation with the ASP.NET MVC BETA 2. Some of the reading that I have done is very critical of the buddy class approach because it isn t DRY (Don t Repeat Yourself) and ...

Mapping db-imported countries to address entity with JPA

I ran some DDL script to setup a complete country table in my database. The country table s primary key column contains the corresponding ISO code for every country. In my JPA project I have a User ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

How to avoid MKMapKit Related Crashes on the iPhone

So, for the past few days I have been struggling to understand how to implement a simple MKMapView with some custom annotations without crashing my application in the process. Unfortunately I have ...