java 一页上的 JSF 多个支持 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1883451/
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
JSF multiple backing beans on one page
提问by Nir Levy
I have done some reading and playing and I still have some questions I was hoping someone might answer:
我已经做了一些阅读和游戏,但我仍然有一些问题希望有人能回答:
So, can I use two or more backing beans in a single JSF page?
那么,我可以在单个 JSF 页面中使用两个或多个支持 bean 吗?
<h:intputText value="#{myFirstBean.firstProperty}" />
<h:intputText value="#{mySecondBean.secondProperty}" />
If I can, why should I not do it? (I assume I should not, because nobody does)
如果可以,我为什么不去做?(我认为我不应该,因为没有人这样做)
If I cannot, why?
如果我不能,为什么?
Also, I read somewhere something like "on page load the framework would instantiate the backing bean, and populate it if it's a postback". They say thebacking bean but I cannot understand how the framework know whichbacking bean to instantiate.
另外,我在某处读到类似“在页面加载时框架将实例化支持 bean,如果它是回发,则填充它”之类的内容。他们说是支持 bean,但我无法理解框架如何知道要实例化哪个支持 bean。
采纳答案by cetnar
Let's clarify some terms:
让我们澄清一些术语:
- managed beansare JavaBeans components that you can configure using the managed bean facility see
- backing beans, are a JavaServer Faces managed beans that are associated with the UI components used in a particular page see
- 托管 bean是 JavaBeans 组件,您可以使用托管 bean 工具对其进行配置,请参阅
- 支持 bean是 JavaServer Faces 托管 bean,与特定页面中使用的 UI 组件相关联,请参阅
So, yes you can use two or more managed beans in a single JSF page, but splitting UI components bindings, listeners, logic etc. that are related to one page into two or more backing beans is still posible but very undesirable and might cause you lot of problems and bad code.
因此,是的,您可以在单个 JSF 页面中使用两个或多个托管 bean,但是将与一个页面相关的 UI 组件绑定、侦听器、逻辑等拆分为两个或多个支持 bean 仍然是可行的,但非常不受欢迎,并且可能会导致您很多问题和糟糕的代码。
回答by Bozho
Why not? It's a perfectly legitimate thing to do.
Generally, a page should be associated with one bean (for the sake of good structure), but if you want, for example, to show the current time on each page, you are free to reference your timeBean.currentTime, among other things (of course, using include/templating is preferable here).
为什么不?这是一件完全合法的事情。一般来说,一个页面应该与一个 bean 相关联(为了良好的结构),但是如果你想,例如,在每个页面上显示当前时间,你可以自由地引用你的timeBean.currentTime,除其他外(当然,在这里最好使用包含/模板)。
回答by Tuukka Mustonen
Other questions have already been answered. However:
其他问题已经得到解答。然而:
Also, I read somewhere something like "on page load the framework would instantiate the backing bean, and populate it if it's a postback". They say the backing bean but I cannot understand how the framework know which backing bean to instantiate.
另外,我在某处读到类似“在页面加载时框架将实例化支持 bean,如果它是回发,则填充它”之类的内容。他们说是支持 bean,但我无法理解框架如何知道要实例化哪个支持 bean。
Beans are resolved by their name. For example, #{myFirstBean.firstProperty}looks for bean named 'myFirstBean' (an instance of class MyFirstBean). You can adjust the name as in:
Bean 由它们的名称解析。例如,#{myFirstBean.firstProperty}查找名为“myFirstBean”的 bean(类的实例MyFirstBean)。您可以调整名称,如下所示:
@ManagedBean(name = "foo")
public class SomeClass { ... }
Then you could reference that via #{foo.firstProperty}.
然后你可以通过#{foo.firstProperty}.

