我可以在同一个xhtml页面中使用多个托管bean吗? [英] Can I use multiple managed bean in the same xhtml page?
问题描述
当数据在几页中重复时(参考示例) 是我可以加载到单个托管bean中,并且在同一页面中使用多个托管bean. 有什么影响?
我可以在同一个xhtml页面中使用多个托管bean吗?
是的,您可以,我强烈建议您尝试一下:).
有什么影响?
这取决于托管bean的作用域.一个很好的例子是查看一个新地址的视图,并在页面顶部显示已记录的用户信息,类似于您在其中添加新送货地址的Amazon网站.
在这种情况下,您可以在三个托管Bean中拥有一个:
- 一个
@SessionScoped
bean,它将显示已记录的用户信息.如果是亚马逊视图,则仅显示名字. - 一个
@ApplicationScoped
bean,它将提供国家/地区的数据.该信息不会经常更改(至少每天都有一个新国家出生==). - 一个
@ViewScoped
bean,它将处理请求数据,错误消息和注册.
Facelets代码中上述解释的示例:
<div id="top">
Hello #{sessionBean.user.firstName}
</div>
<div id="body">
<h1>Add an address</h1>
<h:form id="frmAddress">
<h:panelGrid columns="2">
<h:outputText value="Address" />
<h:inputText id="txtAddress" value="#{viewBean.address}" />
<h:outputText value="Country" />
<h:selectOneMenu id="ddlCountry" value="#{viewBean.selectedCountry}">
<f:selectItems value="#{applicationBean.countries}" var="country"
itemLabel="#{country.name}" itemValue="#{country}" />
</h:selectOneMenu>
</h:panelGrid>
<h:messages id="msgErrors" />
<h:commandButton value="Save address" action="#{viewBean.saveAddress}" />
</h:form>
</div>
注意:这既不是好事也不是坏事,只需尝试一下它的行为即可.影响的定义是页面中每个托管bean的行为,因此根据您定义bean的方式,它会非常整洁或非常糟糕.
相关信息:
when there are data that are repeated in several pages (reference example) Is that I can load into a single managed bean and I use several managed bean in the same page. What is its impact?
Can I use multiple managed bean in the same xhtml page?
Yes, you can, I highly recommend you to try it :).
What is its impact?
This depends on the managed beans scopes. A good example is having a view to register a new address and shows the logged user info at the top of the page, similar to Amazon site where you add a new shipping address.
In this case, you can have among three managed beans:
- A
@SessionScoped
bean that will show the logged user info. In case of amazon view, it shows only the first name. - A
@ApplicationScoped
bean that will provide the data for Countries. This info doesn't change too often (at least that a new country is born every day =\). - A
@ViewScoped
bean that will handle the request data, error messages and the registration.
A sample of the above explanation in Facelets code:
<div id="top">
Hello #{sessionBean.user.firstName}
</div>
<div id="body">
<h1>Add an address</h1>
<h:form id="frmAddress">
<h:panelGrid columns="2">
<h:outputText value="Address" />
<h:inputText id="txtAddress" value="#{viewBean.address}" />
<h:outputText value="Country" />
<h:selectOneMenu id="ddlCountry" value="#{viewBean.selectedCountry}">
<f:selectItems value="#{applicationBean.countries}" var="country"
itemLabel="#{country.name}" itemValue="#{country}" />
</h:selectOneMenu>
</h:panelGrid>
<h:messages id="msgErrors" />
<h:commandButton value="Save address" action="#{viewBean.saveAddress}" />
</h:form>
</div>
Note: this is nor a good nor a bad practice, just give it a try to see how this behaves. The impact is defined how each managed bean behaves in the page, so it will be pretty neat or a really bad experience, based on how you have defined the beans.
Related info:
这篇关于我可以在同一个xhtml页面中使用多个托管bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!