简体   繁体   English

在JSF 2.0组件中包含子元素

[英]Include sub-element inside JSF 2.0 component

This must be simple. 这一定很简单。 I am trying to pass sub-element into a JSF component. 我试图将子元素传递给JSF组件。 I have my component declared as: 我将我的组件声明为:

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

Then I use this in a page by: 然后我在页面中使用它:

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

Unfortunately, the box renders ok (the black border) but the "Hello world!" 不幸的是,盒子呈现好(黑色边框),但“Hello world!” text is not included within it. 文本不包含在其中。 I also tried more verbose syntax by using <ui:insert name="content"> and calling by <ui:define name="content">Hello World!</ui:define> but it didn't work. 我还尝试使用<ui:insert name="content">并通过<ui:define name="content">Hello World!</ui:define>调用更详细的语法,但它不起作用。

Where might I be making a mistake? 我哪里可能犯错?

Ok I figured it out. 好吧我明白了。 You should use <composite:insertChildren /> instead as in: 您应该使用<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>

This works. 这有效。

You have to send the content as parameter: 您必须将内容作为参数发送:

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

and in your code: 在你的代码中:

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

I added the escape="false" since you are using HTML tags inside the EL expression. 我添加了escape="false"因为您在EL表达式中使用HTML标记。

Read more about composite elements in David Geary's article David Geary的文章中阅读有关复合元素的更多信息

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

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