【发布时间】:2013-11-21 22:42:21
【问题描述】:
我的应用程序中有许多复合组件,有时我需要将这些组件作为一个整体进行引用。默认情况下,复合组件不会在其子组件上生成任何附加标记,因此如果您为复合组件提供 id 属性,它只会将该 id 附加到子组件的生成 id 上。我想通过 id 引用整个组件(即用于 jquery 操作)。 JSF 组件不允许在 id 属性中使用 EL,因此为了实现这一点,到目前为止,我一直将我的复合组件包装在纯 html div 中,如下所示:
<ui:component ...
xmlns:cc="http://java.sun.com/jsf/composite"
...>
<cc:interface componentType="myComponent">
<cc:attribute name="process" type="java.lang.String" required="false" default="@form"/>
<cc:attribute name="update" type="java.lang.String" required="false" default="@form"/>
...
</cc:interface>
<cc:implementation>
<h:outputStylesheet name="myComponent.css" library="css/components" target="head"/>
<div id="#{cc.clientId}" class="myComponent #{cc.attrs.styleClass}">
... composite component implementation here ...
<p:dataTable id="note-history" styleClass="entity-notes-history" value="#{cc.notes}" var="note" paginator="true" paginatorAlwaysVisible="false"
paginatorPosition="bottom" rows="5" rendered="#{not empty cc.notes}" rowStyleClass="#{note.noteBaseType.word}">
...
<p:column rendered="#{note.noteBaseType == 'Warning' and not cc.attrs.displayOnly}" style="width: 8em;">
<p:commandButton actionListener="#{cc.cancelSeverity(note.id)}" process="#{cc.attrs.process}" update="#{cc.attrs.update} update="note-history" value="Reduce Severity"/>
</p:column>
</p:dataTable>
</div>
</cc:implementation>
组件的使用方式如下:
....
<ppa:myComponent id="myId" update="myId" />
....
不幸的是,如果我在复合组件中有任何 ajax(特别是来自 primefaces 的 p:ajax)调用,当我尝试让它们按 ID 更新复合组件时,我会收到“找不到具有标识符的组件”异常。有没有办法让 JSF 为我的复合组件自动生成有效的组件和容器?我真的很讨厌必须生成两个包含 div 或 span(即在纯 html div 周围添加一个 h:panelGroup)只是为了让 ajax 工作。或者,有没有办法强制 JSF 允许组件上的动态 id?
编辑:
针对 BallusC 的回答,我编辑了示例代码,以便更清楚地了解组件的构建和使用方式,因为鉴于他的回答,我完全有可能只是在做一些不允许的事情。
【问题讨论】:
标签: ajax jsf jsf-2 primefaces composite-component