java JSF 2:是否可以继承@ManagedBean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6627384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:42:14  来源:igfitidea点击:

JSF 2 : Is it possible to inherit @ManagedBean?

javajsfinheritance

提问by Stephan

I have a Bean,with a @ManagedBeanannotation, defined like this :

我有一个Bean带有@ManagedBean注释的 , 定义如下:


@ManagedBean
@SessionScoped
public class Bean implements Serializable {

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

}

Now, I have another bean defined like this :

现在,我有另一个这样定义的 bean:


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


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


当我尝试在 JSF 页面中引用 FooBean 时,出现以下错误:
Target Unreachable, identifier 'fooBean' resolved to null

Why JSF doesn't see FooBeanas a managed bean ?

为什么 JSF 不被FooBean视为托管 bean?

回答by Rick Garcia

The point Alex is trying to make is that you're confusing classes with instances. This is a classic (pun intended) OOP mistake.

Alex 试图说明的一点是,您将类与实例混淆了。这是一个经典的(双关语)OOP 错误。

The @ManagedBean annotation does not work on classes per-se. It works on instancesof such classes, defining an instance that is managed.

@ManagedBean 注释本身不适用于类。它适用于情况下这样的类的,定义为可管理的实例。

If your bean is defined like this:

如果你的 bean 是这样定义的:

@ManagedBean
@SessionScoped
public class MyBean implements Serializable {

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

Then it means you have a session-scoped instance, called myBean (or whatever you want to name it).

那么这意味着您有一个会话范围的实例,称为 myBean(或您想命名的任何名称)。

Therefore, all classes that are subclasses of the MyBean classare not managed by default. Furthermore, how does JSF recognize where you're using the subclasses? If so, what names are you giving to those instances? (Because you have to give them some name, otherwise, how would JSF manage them?)

因此,默认情况下不管理作为 MyBean类的子类的所有类。此外,JSF 如何识别您在何处使用子类?如果是这样,你给这些实例起什么名字?(因为你必须给它们一些名字,否则,JSF 将如何管理它们?)

So, let's say you have another class:

所以,假设你有另一个类:

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

what happens to the @PostConstruct and all the other annotations you used? Nothing. If you created the instance of MyBean, then it's YOU who manages it, not JavaServerFaces. So it's not really a managed bean, just a common object that you use.

@PostConstruct 和您使用的所有其他注释会发生什么?没有什么。如果您创建了 MyBean 的实例,那么管理它的是您,而不是 JavaServerFaces。所以它不是真正的托管 bean,只是您使用的公共对象。

However, things change completely when you do this:

但是,当您这样做时,情况会完全改变:

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

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

Then again, what is managed is not the class, but the instanceof the class. Having a ManagedBean means that you only have one instance of that bean (per scope, that is).

再说一次,管理的不是类,而是类的实例。拥有 ManagedBean 意味着您只有该 bean 的一个实例(即每个作用域)。

回答by Thang Pham

do you need BaseBeanto be a managed bean? Since you name it BaseBean, I assume that this bean hold commonality between all your other managed bean. If so then it should not contain @ManagedBeanannotation. Do this

你需要BaseBean成为一个托管bean吗?既然你给它命名了BaseBean,我假设这个 bean 在所有其他托管 bean 之间具有共性。如果是这样,那么它不应该包含@ManagedBean注释。做这个

public abstract BaseBean{
    //...
}

Then inside your managed bean

然后在您的托管 bean 中

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