【问题标题】:JSF create an instance of another managedBean [duplicate]JSF创建另一个managedBean的实例[重复]
【发布时间】:2015-08-20 22:48:39
【问题描述】:

我是使用 jsf 框架的新手,我会说中级英语,所以我希望你能理解我的问题。我有这种情况:

这是我的文件 abmEstaciones.xhtml 中的内容

            <ui:define name="contenido">
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">Lista de Estaciones</h3>
        </div>
        <!-- /.box-header -->
        <div style="margin-top: 3%; margin-bottom: 2%;">
            <h:message for="Alta" style="color:green; font-weight: bold;" />
            <h:form id="Alta">
                <h:commandLink styleClass="btn btn-success"
                    action="#{estacionMbReq.visualizarAltaEstacion}">
                    <i class="fa fa-plus-circle"></i>
                    <span class="menu-title">Agregar</span>
                </h:commandLink>
            </h:form> 
        </div>
        <div class="box-body table-responsive">

            <h:outputText value="No hay estaciones" style="color:red; font-weight: bold;" rendered="#{empty estacionMbReq.listaEstaciones}" />
            <h:dataTable id="example1" rendered="#{not empty estacionMbReq.listaEstaciones}" value="#{estacionMbReq.listaEstaciones}"
                var="estacion" styleClass="table table-bordered table-striped">

                <h:column>
                    <f:facet name="header">Nombre</f:facet>
                #{estacion.nombre}
            </h:column>
                <h:column>
                    <f:facet name="header">Total Estacionamientos</f:facet>
                #{estacion.totalEstacionamientos}
            </h:column>
                <h:column>
                    <f:facet name="header">Cant. Estacionamientos libres</f:facet>
                #{estacion.cantEstacionamientosLibres}
            </h:column>
                <h:column>
                    <f:facet name="header">Estado</f:facet>
                #{estacion.estado}
            </h:column>

                <h:column>
                    <f:facet name="header">Opciones</f:facet>
                    <h:form id="formOpcTabla" name="formOpcTabla">
                        <h:commandLink
                            action="#{estacionMbSess.visualizarModificarEstacion}"
                            styleClass="btn btn-primary">
                            <i class="fa fa-edit"></i> Editar
                            <f:param name="idEstacion" value="#{estacion.idEstacion}" />
                        </h:commandLink>
                        <h:commandLink styleClass="btn btn-danger"
                            action="#{estacionMbSess.borradoLogico}">
                            <i class="fa fa-eraser"></i> Eliminar
                        <f:param name="idEstacion" value="#{estacion.idEstacion}" />
                        </h:commandLink>
                    </h:form>
                </h:column>
            </h:dataTable>

        </div>
        <!-- /.box-body -->
    </div>
    <!-- /.box -->
</ui:define>

这是 faces-config.xml 中的导航规则

    <navigation-rule>
    <from-view-id>/abmEstaciones.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{estacionMbReq.visualizarAltaEstacion}</from-action>
        <from-outcome>successVisualizarAltaEstacion</from-outcome>
        <to-view-id>/altaEstacion.xhtml</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-action>#{estacionMbSess.visualizarModificarEstacion}</from-action>
        <from-outcome>successVisualizarModificarEstacion</from-outcome>
        <to-view-id>/modificarEstacion.xhtml</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-action>#{estacionMbSess.borradoLogico}</from-action>
        <from-outcome>listarEstaciones</from-outcome>
        <to-view-id>/abmEstaciones.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

我试图在视图中使用两个托管 bean,一方面是带有 requestScoped 注释的“EstacionMbReq”,另一方面是带有 SessionScoped 注释的“EstacionMbSess”。问题是,当我单击“Editar”或“Eliminar”commandLinks 时,它会执行名为“EstacionMbReq”的 managedBean 的构造函数,而不是我指定的 managedBean:“EstacionMbSess”。

有什么帮助吗?可以在同一个视图中使用两个 managedBeans 吗?谢谢

【问题讨论】:

  • 不确定这是否能帮助您解决问题:Can I use multiple managed bean in the same xhtml page?
  • 另外,我建议您避免在构造函数中编写任何业务逻辑代码,而是使用带有@PostConstruct 修饰的方法。回顾一下 Java web here 中变量作用域的基础知识,JSF 只是重用了这些概念。

标签: java jsf jsf-2.2


【解决方案1】:

构造函数EstacionMbSess在每个HTTP用户会话中只执行一次,并且发生在处理点击请求之前。 EstacionMbReq 是在您每次从该页面发送请求时创建的。

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多