如何在Java Server Faces 2.1中的Composite Component中使用DataTable组件的ID(Primefaces 2.2.1)?
现在,我有一个观点:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:haiq="http://java.sun.com/jsf/composite/haiqcomponents"
template="/WEB-INF/templates/login/main.xhtml">
<ui:define name="content">
<h:form prependId="false"> <!-- prependId="true" ??? -->
<p:dataTable id="leakTable" var="leak" value="#{dataExplorer.data}">
<p:column filterBy="#{leak.source}" headerText="source" footerText="source" filterMatchMode="contains" >
<f:facet name="header">
<h:outputText value="source" />
</f:facet>
<h:outputText value="#{leak.source}" />
</p:column>
<!-- Few more columns here -->
</p:dataTable>
<!-- Add : prefix before ID? -->
<haiq:exporter target=":leakTable" fileName="#{msgs.fileName}" imageLibrary="images" pageOnly="false" />
</h:form>
</ui:define>
</ui:composition>
我的复合组件:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="fileName" default="data" />
<cc:attribute name="target" required="true" type="java.lang.String" />
<cc:attribute name="pageOnly" default="true" type="java.lang.Boolean" />
<cc:attribute name="imageLibrary" default="images" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:commandLink>
<h:graphicImage library="#{cc.attrs.imageLibrary}" name="excel.png" />
<p:dataExporter type="xls"
target="#{cc.attrs.target}"
fileName="#{cc.attrs.filename}"
pageOnly="#{cc.attrs.pageOnly}" />
</h:commandLink>
</cc:implementation>
</html>
视图渲染后,发生以下错误:
javax.faces.FacesException: Cannot find component ":leakTable" in view.
at org.primefaces.component.export.DataExporter.processAction(DataExporter.java:89)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
删除:在leakTable之前(在target属性中)或将preperndId(在表单中)更改为true并不能解决问题。
如何在cc中使用数据表? 类似的问题被描述为here
答案 0 :(得分:2)
显然,您在视图中还有另一个NamingContainer
父级。确保在浏览器中打开页面,右键单击查看源并确定生成的<p:dataTable id="leakTable">
ID。然后,您应该使用:
准确抓取该ID和前缀。
或者,您也可以将表组件绑定到视图,然后使用UIComponent#getClientId()
来动态引用客户端ID。
<p:dataTable binding="#{leakTable}" ...>
与
<haiq:exporter target=":#{leakTable.clientId}" ...>
无关,我建议用<ui:component>
替换你的复合的<!DOCTYPE><html>
,这更自然,它也可以保证JSF每次都不会这样做。
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:p="http://primefaces.prime.com.tr/ui">