简体   繁体   English

如何为数据表列创建复合组件?

[英]How to create a composite component for a datatable column?

Given this datatable (naturally working):鉴于此数据表(自然工作):

<rich:dataTable var="var" value="#{values}">
<rich:column>
  <f:facet name="header">
   HEADER
  </f:facet>
  <h:outputText value="#{var}" />
</rich:column>
</rich:dataTable>

If I define a custom component (also ok in the syntax and at the right place under resources/components):如果我定义了一个自定义组件(在语法和资源/组件下的正确位置也可以):

   <?xml version="1.0" encoding="UTF-8"?>
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:a4j="http://richfaces.org/a4j"
        xmlns:rich="http://richfaces.org/rich"
        xmlns:composite="http://java.sun.com/jsf/composite">

    <!-- INTERFACE -->
    <composite:interface>
        <composite:attribute name="val" />
    </composite:interface>

    <!-- IMPLEMENTATION -->
    <composite:implementation>
        <rich:column>
            <f:facet name="header">
       HEADER
       </f:facet>
       <h:outputText value="#{cc.attrs.val}" />
       </rich:column>
    </composite:implementation>
    </html>

Why does the following does not work?为什么以下不起作用?

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="/WEB-INF/templates/default.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:my="http://java.sun.com/jsf/composite/components">
    <ui:define name="content">
        <rich:dataTable var="var" value="#{values}">
                 <my:mycolumn val="#{var}"/>
                </rich:dataTable>
    </ui:define>
</ui:composition>

Do you know how could I let it work (I want to define my own column and save code)?你知道我怎么让它工作(我想定义我自己的列并保存代码)? Thanks a lot!非常感谢! Bye再见

The <my:mycolumn> element must be an instance of UIColumn as that's the only valid child of a UIData component during the render response phase. <my:mycolumn>元素必须是UIColumn一个实例,因为它是呈现响应阶段UIData组件的唯一有效子元素。 All other UIComponent types will be ignored, thus not rendered.所有其他UIComponent类型将被忽略,因此不会呈现。 A composite component is implicitly a UINamingContaner component, which isn't a UIColumn and therefore ignored.复合组件隐式是UINamingContaner组件,它不是UIColumn ,因此被忽略。

A PrimeFaces <p:dataTable> with a backing component that extends UIColumn also won't work due to the wrong lifecycle of a composite component.由于复合组件的错误生命周期,带有扩展UIColumn的支持组件的 PrimeFaces <p:dataTable>也将无法工作。 The column has to be created during the view build time, while the composite component's body is created during view render time.必须在视图构建期间创建列,而在视图渲染期间创建复合组件的主体。

The solution is to create a tag file instead, which means an extra .taglib.xml file, yet it works flawlessly.解决方案是创建一个标记文件,这意味着一个额外的.taglib.xml文件,但它可以完美地工作。

/WEB-INF/tags/column.xhtml : /WEB-INF/tags/column.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:rich="http://richfaces.org/rich">
    <rich:column>
        <f:facet name="header">HEADER</f:facet>
        <h:outputText value="#{val}" />
    </rich:column>
</ui:composition>

/WEB-INF/my.taglib.xml : /WEB-INF/my.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0">
    <namespace>http://example.com/my</namespace>
    <tag>
        <tag-name>column</tag-name>
        <source>tags/column.xhtml</source>
        <attribute>
            <description>Column value</description>
            <name>val</name>
        </attribute>
    </tag>
</facelet-taglib>

Note: The <attribute> entries are not mandatory, but are nice for documentation purposes, such as generated docs and IDE autocomplete.注意: <attribute>条目不是强制性的,但对于文档目的来说很好,例如生成的文档和 IDE 自动完成。

/WEB-INF/web.xml : /WEB-INF/web.xml

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>

Usage:用法:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:rich="http://richfaces.org/rich"
    xmlns:my="http://example.com/my">
    <rich:dataTable value="#{values}" var="value">
        <my:column val="#{value}" />
    </rich:dataTable>
</ui:composition>

I ran into the same question (primefaces), my compromise: exclude the column tag from the composite我遇到了同样的问题(primefaces),我的妥协:从组合中排除列标签

<rich:dataTable var="var" value="#{values}">
    <rich:column rendered=#{yesOrNo}">
       <my:mycolumn val="#{var}">
       .. with child nodes if you want ...
       </my:mycolumn>
    </rich:column>
</rich:dataTable>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM