简体   繁体   English

一页上的JSF多个支持bean

[英]JSF multiple backing beans on one page

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". 此外,我在某处读到类似“在页面加载框架将实例化支持bean,并填充它,如果它是一个回发”。 They say the backing bean but I cannot understand how the framework know which backing bean to instantiate. 他们说支持 bean,但我不明白的框架如何知道哪些支持bean实例。

Let's clarify some terms: 让我们澄清一些术语:

  • managed beans are JavaBeans components that you can configure using the managed bean facility see 管理豆 ,你可以使用配置管理bean设施JavaBeans组件
  • backing beans , are a JavaServer Faces managed beans that are associated with the UI components used in a particular page see 支持豆 ,都是在JavaServer Faces中的与特定页面中使用的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仍然是可行的,但非常不受欢迎,可能会导致您很多问题和糟糕的代码。

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等等(当然,这里最好使用include / templating)。

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". 此外,我在某处读到类似“在页面加载框架将实例化支持bean,并填充它,如果它是一个回发”。 They say the backing bean but I cannot understand how the framework know which backing bean to instantiate. 他们说支持bean但我无法理解框架如何知道实例化哪个支持bean。

Beans are resolved by their name. 豆类的名称已经解决了。 For example, #{myFirstBean.firstProperty} looks for bean named 'myFirstBean' (an instance of class MyFirstBean ). 例如, #{myFirstBean.firstProperty}查找名为“myFirstBean”的bean(类MyFirstBean的实例)。 You can adjust the name as in: 您可以调整名称,如下所示:

@ManagedBean(name = "foo")
public class SomeClass { ... }

Then you could reference that via #{foo.firstProperty} . 然后你可以通过#{foo.firstProperty}引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM