3

我写了一个自定义标签扩展UIComponentBase.
它在方法期间添加了多个子组件(UIComponentencodeBegin

出于布局目的,我想将此 Child-Components 嵌套在 a 中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>

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

4

1 回答 1

3
  1. h:panelGrid只控制其自己的孩子的布局(而不是其孩子的孩子)
  2. 每个都<foo:exampleTag />创建一个复合控件(带有自己的子控件)

如果要向 中添加多个控件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>

包含的合成文件:

<!-- 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>

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


组件不是标签。

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

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

于 2011-05-09T17:21:49.813 回答