我写了一个扩展 UIComponentBase
的自定义标签。
它在 encodeBegin
方法期间添加多个子组件 (UIComponent
)。
出于布局目的,我想将此子组件嵌套在 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
组件,
但我想让它们排成一排,即两个单元格。
最佳答案
-
h:panelGrid
只控制它自己的 child 的布局(而不是它的 child 的 child ) - 每个
<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>
包含 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>
View 输出的一个子集:
<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();
}
这不是构建复合控件的可接受方式。如果这样做,每次渲染时都会将新控件添加到组件中。您也不应该在构造函数中这样做;那也会导致问题。在控件中添加子控件没有好的方法;它应该由 View (见上文)或 a tag 在外部完成.
关于java - h :panelGrid 中具有多个子组件的自定义 Facelets 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938640/