【问题标题】:Using one component (dataTable) id inside composite component在复合组件内使用一个组件 (dataTable) id
【发布时间】:2011-12-19 16:39:35
【问题描述】:

如何在 Java Server Faces 2.1 的复合组件中使用 DataTable 组件 (Primefaces 2.2.1) 的 id ?

现在,我有一个看法:

<?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)

Removing : before leakTable (in target attribute) 或改变 preperndId (in form) 为 true 并不能解决问题。

如何在 cc 中使用数据表? 类似问题描述here

【问题讨论】:

    标签: jsf jsf-2 primefaces facelets composite-component


    【解决方案1】:

    显然,您的视图中有另一个 NamingContainer 父级。可以肯定的是,在浏览器中打开页面,右键单击并查看源代码,然后确定生成的 ID 为&lt;p:dataTable id="leakTable"&gt;。然后,您应该准确获取该 ID 并以 : 为前缀。

    或者,您也可以将表格组件绑定到视图,并使用UIComponent#getClientId() 来动态引用客户端 ID。

    <p:dataTable binding="#{leakTable}" ...>
    

    与

    <haiq:exporter target=":#{leakTable.clientId}" ...>
    

    与具体问题无关,我建议用&lt;ui:component&gt; 替换您的复合材料中的&lt;!DOCTYPE&gt;&lt;html&gt;,这样更自然,也可以避免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">
    

    另见https://stackoverflow.com/tags/composite-component/info。

    【讨论】:

    • 感谢您的回复!不知道怎么绑定ID。我试图声明私有 UIComponent nameField = new UIInput();使用 getter 和 setter 并通过 #{dataExplorer.nameField} 使用它,但这会产生更多问题,例如 java.lang.IllegalArgumentException: ""。我检查了 p:dataTable 生成的 HTML,并且在 div 和脚本中有 leakTable:... ids。
    • 呃,我已经在我的回答中用一些代码演示了它。只需复制粘贴即可。您不需要将其绑定到某些托管 bean 属性。只需将其直接绑定到视图即可。完全如我的回答所示。
    • 啊,我使用了您的解决方案,但是...我正在使用自动部署 glassfish。这个feature 给我带来了很多问题——点击 cc 页面后重新加载,没有任何反应。我重新启动了 Glassfish 并再次部署了 webapp,然后您的解决方案就奏效了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 2012-05-08
    • 2013-11-12
    • 2012-08-30
    • 2016-01-20
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    相关资源
    最近更新 更多