在复合组件内使用一个组件(dataTable)id

发布于 2024-12-13 06:54:47 字数 3479 浏览 0 评论 0原文

如何在 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)

删除:在leakTable(在目标属性中)之前或将preperndId(在表单中)更改为true并不能解决问题。

如何在cc中使用数据表? 此处描述了类似的问题

How can I use id of DataTable Component (Primefaces 2.2.1) inside Composite Component in Java Server Faces 2.1 ?

Now, I have a view:

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

My composite component:

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

After view rendering, following error occurred:

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) or changing preperndId (in form) to true does not solve the problem.

How can I use datatable inside cc?
Similar problem is described here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-12-20 06:54:47

显然你还有另一个 NamingContainer 视图中的父级。为确保安全,请在浏览器中打开页面,右键单击并查看源代码,然后确定生成的 ID。然后,您应该准确获取该 ID 并使用 : 前缀。

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

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

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

具体问题无关,我建议将复合材料的 替换为 ,这更自然,而且还可以避免 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

Apparently you've another NamingContainer parent in the view. To be sure, open page in browser, rightclick and View Source and determine the generated ID of <p:dataTable id="leakTable">. Then, you should grab exactly that ID and prefix with :.

Alternatively, you can also bind the table component to the view and use UIComponent#getClientId() instead to dynamically refer the client ID.

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

with

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

Unrelated to the concrete problem, I suggest to replace <!DOCTYPE><html> of your composite by <ui:component>, which is more natural and it also saves JSF from doing it impliticly everytime.

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

See also https://stackoverflow.com/tags/composite-component/info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文