将辅助bean实例作为参数传递给复合组件

时间:2014-06-03 15:02:42

标签: jsf el jsf-2.2 composite-component

是否可以使用JSF 2.2将支持bean实例作为参数传递给复合组件并调用bean实例中的方法?

有没有例子?谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

我明白了!这是我的解决方案:

复合组件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:cc="http://java.sun.com/jsf/composite">

    <cc:interface>
        <cc:attribute name="backingBean" required="true" shortDescription="A backing bean to invoke polimorphic methods."/>
    </cc:interface>

    <cc:implementation>
        <h:commandButton value="Foo" action="#{cc.attrs.backingBean.myPolimorphicMethod}"/>     
    </cc:implementation>

</html>

使用复合组件:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite/components"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/templates/template.xhtml">

    <ui:define name="content">
        <h:form>
            <cc:myCustomComponent backingBean="#{myBacking}"></cc:myCustomComponent>
        </h:form>
    </ui:define>

</ui:composition>