【问题标题】:Include sub-element inside JSF 2.0 component在 JSF 2.0 组件中包含子元素
【发布时间】:2011-03-29 07:58:36
【问题描述】:

这一定很简单。我正在尝试将子元素传递给 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!”文本不包含在其中。我还尝试了使用&lt;ui:insert name="content"&gt;&lt;ui:define name="content"&gt;Hello World!&lt;/ui:define&gt; 调用的更详细的语法,但它不起作用。

我可能会在哪里犯错?

【问题讨论】:

    标签: java jsf components wrapper parameter-passing


    【解决方案1】:

    好的,我想通了。你应该使用&lt;composite:insertChildren /&gt; 代替:

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

    这行得通。

    【讨论】:

      【解决方案2】:

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

      <?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’s article中阅读有关复合元素的更多信息

      【讨论】:

      • 是的,这将是一种方法,但它的设计非常丑陋。另外,如果我有一段较长的 XHTML/JSF 代码要用作子元素怎么办?将内容作为字符串属性传递是不方便的。相反,我尝试的样式(使用&lt;ui:insert /&gt;)应该可以工作,如packtpub.com/article/creating-composition-components-in-jsf-2.0 中所述(请参阅“将子元素传递给组合组件”一章)。 JSF 2 APIs 和 JBoss Seam 一书中也介绍了同样的方法。但是,在我的测试中它失败了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多