如何在JSF中创建现有组件的组合?

mar*_*zzz 6 jsf components facelets

我想知道是否有可能组成我自己的组件(或称之为Widget,Object).

我的意思是,而不是(例如)使用h:panelGroup和在h:outputLabel里面,使用我自己的h:panelMarkzzz,作为panelGroup和outputLabel的组合.

在JSF上有可能吗?

Bal*_*usC 7

是的,可以创建这样的现有组件的组合.

开球示例:

/resources/foo/group.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite">
    <cc:interface>
        <cc:attribute name="label" type="java.lang.String" required="true" />
    </cc:interface>
    <cc:implementation>
         <h:panelGroup>
             <h:outputLabel value="#{cc.attrs.label}" />
             <cc:insertChildren />
         </h:panelGroup>
    </cc:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)

/test.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://xmlns.jcp.org/jsf/html" 
    xmlns:foo="http://xmlns.jcp.org/jsf/composite/foo">
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>
        <foo:group label="Label value">
            <h:outputText value="This will appear after label inside the panelgroup" />
        </foo:group>
    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

/foo文件夹的名称是自由你的口味,你可以在XML命名空间中引用它http://xmlns.jcp.org/jsf/composite/XXX.XHTML文件名是标记名称.

也就是说,复合组件具有性能影响,只有在使用简单的include或tagfile无法实现功能要求时才能使用它们.在您的特定情况下,您最好使用标记文件.复合组件只有在您真正需要它时才值得<cc:interface componentType="...">.

也可以看看: