2011-06-09 55 views
2

我一直在玩复合组件,我遇到了一些我找不到的东西。如果不使用复合材料部件我想有这样的事情:即代码插入复合材料部件的JSF复合组件和selectItems

<h:outputText value="Some Label:"/> 
<h:selectOneMenu"> 
    <f:selectItem itemLabel="Item 1"/> 
    <f:selectItem itemLabel="Item 2"/> 
    <f:selectItem itemLabel="Item 3"/> 
</h:selectOneMenu> 

我基本上打开:

<!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:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:composite="http://java.sun.com/jsf/composite"> 

    <!-- INTERFACE --> 
    <composite:interface> 
    <composite:attribute name="label" required="true"/> 
    <composite:attribute name="options" required="true"/>  
    </composite:interface> 

    <!-- IMPLEMENTATION -->   
    <composite:implementation> 
    <h:outputText value="#{cc.attrs.label}"/> 
    <h:selectOneMenu style="width:140px;"> 
     <f:selectItem value="#{cc.attrs.options}" /> 
    </h:selectOneMenu> 
    </composite:implementation> 
</html> 

现在这里是我难倒。我知道我可以把我想的成分在支持bean使用,使用它像这样的项目清单:

<util:dropdown label="Some Label:" options="#{backingBean.list}"/> 

真正的问题是,如果有一种方法的选项来传递,而不必使用支持bean?我想是这样的:

<util:dropdown label="Some Label:" options="Item 1, Item 2, Item 3"/> 

也许

<util:dropdown label="Some Label:" options="#{backingBean.list}"> 
    <f:selectItem itemLabel="Item 1"/> 
    <f:selectItem itemLabel="Item 2"/> 
    <f:selectItem itemLabel="Item 3"/> 
</util:dropdown> 

注:显然这两个不工作,或者我就不会问这个问题。

回答

2

真正的问题是,如果有一种方法可以传递选项而无需使用支持bean?我想是这样的:

<util:dropdown label="Some Label:" options="Item 1, Item 2, Item 3"/> 

您可以使用JSTL fn:split()将它们分成String[]<f:selectItems>也接受为value。我只能删除options属性中逗号周围的空格。

xmlns:fn="http://java.sun.com/jsp/jstl/functions" 
... 

<h:selectOneMenu> 
    <f:selectItems value="#{fn:split(cc.attrs.options, ',')}" /> 
</h:selectOneMenu> 

也许

<util:dropdown label="Some Label:"> 
    <f:selectItem itemLabel="Item 1"/> 
    <f:selectItem itemLabel="Item 2"/> 
    <f:selectItem itemLabel="Item 3"/> 
</util:dropdown> 

您可以使用<composite:insertChildren>在复合组件声明的位置,其中复合材料部件的孩子必须被插入。

<h:selectOneMenu> 
    <composite:insertChildren /> 
</h:selectOneMenu> 
+0

说我需要的复合材料部件插入两套不同的孩子在两个不同的地方 - 我将如何做到这一点,否则?我只能使用第一个选项。我试图将f:selectItems放在一个构面中,然后在复合组件中渲染该构面,但这也不起作用。在非复合组件的情况下,即只使用ui:decorate,我可以指定多个ui:为ui定义:按名称插入多个子项。为什么复合组件不具备此功能? – GreenieMeanie 2015-04-09 22:49:40

+0

@GreenieMeanie:复合材料支持方面。 – BalusC 2015-04-10 05:59:33