JSF 2: Is it possible to inherit @ManagedBean?

I have Bean, with @ManagedBean annotation defined as follows:


@ManagedBean
@SessionScoped
public class Bean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

Now I have another bean defined as follows:


public class FooBean extends Bean {
    // properties, methods here ...
}


When I try to link to FooBean in my JSF page, I have the following error:
Target Unreachable, identifier 'fooBean' resolved to null

Why doesn't JSF see FooBeanhow a managed bean is?

+7
source share
2 answers

The point that Alex is trying to make is that you confuse classes with instances. This is a classic (intended for casting) OOP error.

@ManagedBean per se. , .

bean :

@ManagedBean
@SessionScoped
public class MyBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
}

, , myBean ( , ).

, MyBean > , . , JSF , ? , ? ( - , JSF ?)

, , :

Class MyOtherClass {
    private MyBean myBeanObject; // myBeanObject isn't managed. 
}

@PostConstruct , ? . MyBean, , , JavaServerFaces. , bean, , .

, , :

@ManagedBean
@SessionScoped
Class MyOtherClassBean {
    @ManagedProperty("#{myBean}")
    private MyBean myBeanObject;

    public void setMyBeanObject(...) { ... }
    public MyBeanClass getMyBeanObject() { ... }
}

, . ManagedBean , bean ( ).

+9

BaseBean bean? BaseBean, , bean bean. , @ManagedBean.

public abstract BaseBean{
    //...
}

bean

@ManagedBean
@RequestScoped
public class FooBean extends BaseBean{
    //...
}
+5

All Articles