3

複合コンポーネントをいじっていて、わからないことがありました。複合コンポーネントを使用しないと、次のようになります。

<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つは機能しません。そうでない場合、私は質問をしません。

4

1 に答える 1

2

本当の問題は、バッキングBeanを使用せずにオプションを渡す方法があるかどうかです。私は次のようなものを推測します:

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

JSTLfn:split()を使用して、それらをに分割できますString[]<f:selectItems>もそれをとして受け入れますvalueoptions属性のコンマの周りのスペースのみを削除します。

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> 
于 2011-06-09T20:50:41.703 に答える