自定义Facelets - 在h:panelGrid中标记多个子组件

时间:2011-05-09 14:54:43

标签: java jsf tags facelets uicomponents

我写了一个自定义标记UIComponentBase 它在UIComponent方法中添加了多个子组件(encodeBegin)。

出于布局目的,我想将这个子组件嵌套在h:panelGrid中 但是标签会妨碍这里。

ExampleTag.java

private ExampleTag extends UIComponentBase {

    public void encodeBegin(FacesContext context) throws IOException {
        getChildren().add(new HtmlLabel());
        getChildren().add(new HtmlOutputText();
    }
}

ExampleOutput.xhtml

<html>
    <h:panelGrid columns="2">
       <foo:exampleTag />
       <foo:exampleTag />
    </h:panelGrid>
</html>

生成的输出将包含相同单元格中的HtmlLabelHtmlOutput个组件
但是我希望将它们放在一行,即两个单元

1 个答案:

答案 0 :(得分:3)

  1. h:panelGrid仅控制自己孩子的布局(而不是孩子的孩子)
  2. 每个<foo:exampleTag />创建一个复合控件(带有自己的子项)
  3. 如果要向h:panelGrid添加多个控件,请使用其他模板机制之一。

    例如,此h:panelGrid使用ui:include

        <h:panelGrid columns="2">
          <ui:include src="gridme.xhtml">
            <ui:param name="foo" value="Hello,"/>
            <ui:param name="bar" value="World!"/>
          </ui:include>
          <ui:include src="gridme.xhtml">
            <ui:param name="foo" value="Hello,"/>
            <ui:param name="bar" value="Nurse!"/>
          </ui:include>
        </h:panelGrid>
    

    包含的composition文件:

    <!-- gridme.xhtml -->
    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html">
      <h:outputText value="#{foo}" />
      <h:outputText value="#{bar}" />
    </ui:composition>
    

    视图输出的子集:

    <table>
    <tbody>
    <tr>
    <td>Hello,</td>
    <td>World!</td>
    </tr>
    <tr>
    <td>Hello,</td>
    <td>Nurse!</td>
    </tr>
    </tbody>
    </table>
    

    注意上面的实现 - 您不能在gridme.xhtml中的任何内容上显式设置ID,因为没有复合控件,因此没有NamespaceContainer来确保子项被唯一地命名空间。


    组件不是标签。

    public void encodeBegin(FacesContext context) throws IOException {
      getChildren().add(new HtmlLabel());
      getChildren().add(new HtmlOutputText();
    }
    

    这是构建复合控件的可接受方式。如果这样做,每次渲染时都会向组件添加新控件。你也不应该在构造函数中这样做;这也会导致问题。没有好的方法在控件中添加子控件;它应该由视图外部完成(见上文)或a tag

相关问题