[英]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.