我可以在同一个 xhtml 页面中使用多个托管 bean 吗? [英] Can I use multiple managed bean in the same xhtml page?

查看:30
本文介绍了我可以在同一个 xhtml 页面中使用多个托管 bean 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有数据在多页中重复时(参考示例)是我可以加载到单个托管 bean 中,并在同一页面中使用多个托管 bean.它有什么影响?

解决方案

我可以在同一个 xhtml 页面中使用多个托管 bean 吗?

是的,你可以,我强烈建议你尝试一下:)

<块引用>

它的影响是什么?

这取决于托管 bean 范围.一个很好的例子是注册一个新地址并在页面顶部显示登录的用户信息,类似于您添加新送货地址的亚马逊网站.

在这种情况下,您可以拥有三个托管 bean:

  • 一个 @SessionScoped bean,将显示登录的用户信息.在亚马逊视图的情况下,它只显示名字.
  • 一个 @ApplicationScoped bean,将为国家/地区提供数据.此信息不会经常更改(至少每天都有一个新国家诞生 =).
  • 一个 @ViewScoped bean,它将处理请求数据、错误消息和注册.

Facelets 代码中上述解释的示例:

你好#{sessionBean.user.firstName}

<div id="body"><h1>添加地址</h1><h:form id="frmAddress"><h:panelGrid columns="2"><h:outputText value="地址"/><h:inputText id="txtAddress" value="#{viewBean.address}"/><h:outputText value="国家/地区"/><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="保存地址" action="#{viewBean.saveAddress}"/></h:form>

注意:这既不是好的也不是坏的做法,只是尝试看看它是如何表现的.影响是定义了每个托管 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆