繁体   English   中英

在JSF 2.0组件中包含子元素

[英]Include sub-element inside JSF 2.0 component

这一定很简单。 我试图将子元素传递给JSF组件。 我将我的组件声明为:

<?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:ui="http://java.sun.com/jsf/facelets"
    xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface>
</composite:interface>

<composite:implementation>
    <div style="border: 1px solid black;">
        <ui:insert />
    </div>
</composite:implementation>

</html>

然后我在页面中使用它:

<box:box>
    <p>Hello world!</p>
</box:box>

不幸的是,盒子呈现好(黑色边框),但“Hello world!” 文本不包含在其中。 我还尝试使用<ui:insert name="content">并通过<ui:define name="content">Hello World!</ui:define>调用更详细的语法,但它不起作用。

我哪里可能犯错?

好吧我明白了。 您应该使用<composite:insertChildren />而不是:

<?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:ui="http://java.sun.com/jsf/facelets"
    xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface>
</composite:interface>

<composite:implementation>
    <div style="border: 1px solid black;">
        <composite:insertChildren />
    </div>
</composite:implementation>

</html>

这有效。

您必须将内容作为参数发送:

<?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:ui="http://java.sun.com/jsf/facelets"
        xmlns:composite="http://java.sun.com/jsf/composite">

    <composite:interface>
         <composite:attribute name="content"/>
    </composite:interface>

    <composite:implementation>
        <div style="border: 1px solid black;">
             <h:outputText value="#{cc.attrs.content}" escape="false"/>
        </div>
    </composite:implementation>

</html>

在你的代码中:

<box:box content="<p>Hello world!</p>" />

我添加了escape="false"因为您在EL表达式中使用HTML标记。

David Geary的文章中阅读有关复合元素的更多信息

暂无
暂无

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

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