问题描述
给定这个datatable(自然工作): < rich:dataTable var =varvalue =#{值}>
< rich:column>
< f:facet name =header>
HEADER
< / f:facet>
< h:outputText value =#{var}/>
< / rich:column>
< / rich:dataTable>
如果我定义一个自定义组件(也可以在语法和正确的地方在资源/组件):
<?xml version =1.0encoding =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>
<! - 实现 - >
< composite:implementation>
< rich:column>
< f:facet name =header>
HEADER
< / f:facet>
< h:outputText value =#{cc.attrs.val}/>
< / rich:column>
< / composite:implementation>
< / html>
为什么以下内容不起作用?
<?xml version =1.0encoding =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 =varvalue =#{values}>
< my:mycolumn val =#{var}/>
< / rich:dataTable>
< / ui:define>
< / ui:composition>
你知道我该如何让它工作(我想定义我自己的列并保存代码) ?非常感谢!再见
这不行。 < my:mycolumn>
真的需要是一个 UIColumn
的实例,因为只有有效的孩子 UIData
组件在渲染响应阶段。所有其他 UIComponent
类型将被忽略。一个复合组件隐含地是一个 UINamingContaner
组件,而不会按照你的意图工作。
我有自己为PrimeFaces < p:dataTable>
曾尝试使用扩展 UIColumn
的支持组件,但也只是由于复合组件的生命周期错误,将无法正常工作。列必须在视图构建时创建,而复合组件的正文在视图渲染时间内创建。
相反,创建一个标记文件。它只花费一个额外的 .taglib.xml
文件,但它完美无缺。
/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
:
<?xml version =1.0encoding =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>列< / tag-name>
< source> tags / column.xhtml< / source>
< attribute>
< description>列值< / description>
< name> val< / name>
< / attribute>
< / tag>
< / facelet-taglib>
(< attribute>
条目不是强制性的,它只是纪录目的(生成的文档和IDE自动完成)。
/ 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>
用法:
code>< 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>
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
This won't work. The <my:mycolumn>
really needs to be an instance of UIColumn
as that's the only valid child of UIData
component during render response phase. All other UIComponent
types will be plain ignored. A composite component is implicitly an UINamingContaner
component and that just won't work the way you intend.
I have myself for the PrimeFaces <p:dataTable>
ever experimented with a backing component which extends UIColumn
, but that also just won't work due to the wrong lifecycle of a composite component. The column has to be created during the view build time, while the composite component's body get created during view render time.
Rather create a tag file instead. It only costs only one extra .taglib.xml
file, but it works flawlessly.
/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
:
<?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>
(the <attribute>
entries are not mandatory, it's just nice for documentary purposes (generated docs and IDE autocomplete))
/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>
这篇关于如何为datatable列创建复合组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!