【问题标题】:Parameterizing managed bean instance as composite component attribute将托管 bean 实例参数化为复合组件属性
【发布时间】:2014-09-01 15:27:44
【问题描述】:

我一直在搜索如何参数化 Managed Bean 类,但直到现在都没有。

我在做什么?

我有一个 JSF 组件可以从我的 Bean 中访问一些方法,但是这些方法已经由一个抽象类实现了。

方法和属性的名称没有改变,但是现在我在复制粘贴,只改变了 Bean 的名称。

第一个实现是可行的,但我想改进第二个。

1- 组件(现在):

...
<cc:interface>
    <cc:attribute name="title"         type="java.lang.String"  required="true"/>
    <cc:attribute name="data"          type="java.util.List"    required="true"/>
    <cc:attribute name="columnModel"   type="java.util.List"    required="true"/>
    <cc:attribute name="newEntity"     type="java.lang.Object"  required="true"/>
    <cc:attribute name="crudState"     type="java.lang.Integer" required="true"/>
</cc:interface>
...
<cc:implementation>
    <p:dataTable var="entity" value="#{cc.attrs.data}" >
        <p:columns value="#{cc.attrs.columnModel}" 
            var="column" style="#{column.style}" 
            styleClass="#{column.styleClass}">
        ...
        </p:columns>
    </p:dataTable>
</cc:implementation>
...

1- 实现(现在):

    ...
    <comp:crud 
        title="#{cfgUserBean.title}"
        data="#{cfgUserBean.data}"
        columnModel="#{cfgUserBean.coluuns}"
        newEntity="#{cfgUserBean.newEntity}"
        newEntity="#{cfgUserBean.crudState}"/>
    ...
    <comp:crud 
        title="#{cfgCityBean.title}"
        data="#{cfgCityBean.data}"
        columnModel="#{cfgCityBean.columns}"
        newEntity="#{cfgCityBean.newEntity}"
        crudState="#{cfgCityBean.curdState}"/>

期望:

将 Bean 名称作为参数传递

2- 组件(所需):

...
<cc:interface>
    <cc:attribute name="BEANNAME" type="java.lang.Object"  required="true"/>
</cc:interface>
...
<cc:implementation>
    <p:dataTable var="entity" value="#{cc.attrs.BEANNAME.data}" >
        <p:columns value="#{cc.attrs.BEANNAME.columnModel}" 
            var="column" style="#{column.style}" 
            styleClass="#{column.styleClass}">
        ...
        </p:columns>
    </p:dataTable>
</cc:implementation>

2- 实现(期望):

    ...
    <comp:crud BEANNAME="cfgUserBean" />
    ...
    <comp:crud BEANNAME="cfgCityBean" />

结论

如您所见,如果我可以将 Bean 名称参数化,我将能够大大简化最终编码。

任何有任何想法的人我能做什么? 提前谢谢你

【问题讨论】:

    标签: jsf jsf-2 composite-component


    【解决方案1】:

    在@BalusC 的帮助下澄清了我的想法,我注意到第二个实现几乎回答了我的问题。

    所以,我做了什么:

    组件

    ...
    <cc:interface>
        <cc:attribute name="managedBean"  type="company.AbstractWebCrud" required="true"/>
    </cc:interface>
    ...
    <cc:implementation>
        <p:dataTable var="entity" value="#{cc.attrs.managedBean.data}" >
        ...
        </p:dataTable>
    </cc:implementation>
    ...
    

    实施

    ...
    <comp:crud-lista 
        managedBean="#{cfgUserBean}"/>
    ...
    

    结论

    将抽象类作为参数类型传递,我能够访问该类中的所有公共方法。像魅力一样工作!

    感谢@BalusC 的关注!! :-)

    【讨论】:

      猜你喜欢
      • 2011-09-29
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2012-11-08
      相关资源
      最近更新 更多