2009-08-28 15 views

答えて

2

私はこれを理解しましたが、JSFだけでなくRichFacesのAJAX機能も使用しました。ちょうど私の最初のselectOneMenuにタグを追加し、それが動作します:)

<a4j:support event="onchange" action="#{bean.onChange}" 
      reRender="otherSelectOneMenuID"/> 

とにかく応答をありがとう!

+0

あなたはそれが働いてうれしいです。 RichFacesは、標準のJSFよりも簡単に処理できます。 –

1

値変更リスナーを最初のselectOneMenuにバインドすると可能です。

ValueChangeEventから新しい値を取得し、それに応じてリストを更新します。 JSFページに更新されたリストが表示されます。

希望は意味があります!

+0

私はそれを試してみます。ありがとう! –

+0

ValueChangeEventは、ページを送信したときにのみ呼び出されるようです:\ –

+0

私のプロジェクトにはrichFacesもありますので、ここでAJAXの機能を使用する必要がありますか? –

0

よく私はa4jを使用していました。

<code> 
//JSF 
<h:outputLabel value="First selectOneMenu: "/> 
<h:selectOneMenu value="#{yourBackingBean.selectedItem}"> 
<f:converter converterId="defaultConverter"/> 
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" /> 
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" /> 
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" /> 
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change 
</h:selectOneMenu> 


<h:outputLabel value="Second selectOneMenu: "/> 
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu"> 
<f:converter converterId="defaultConverter"/> 
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value 
<f:selectItems value="#{yourBackingBean.returnByChoice}" /> 
</h:selectOneMenu> 


//Converter 

public class DefaultConverter implements Converter { 
public Object getAsObject(FacesContext ctx, UIComponent component, String value) { 
    return value; 
} 

public String getAsString(FacesContext ctx, UIComponent component, Object value) { 
    String label = ""; 
    if (value != null) { 
     label = value.toString(); 
    } 
    return label; 
} 
} 

//Backing Bean Sample 
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page 
    String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu. 
    ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>(); 
    if (id != null) { 

      List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id)); 

     Iterator<YourClass> iterator = yourObjectList.iterator(); 
     String tempName = ""; 
     String tempId = ""; 
     YourClass tempYourObject = null; 

     while (iterator.hasNext()) { 
      tempYourObject = iterator.next(); 
      tempId = String.valueOf(tempYourObject.getId()); 
      tempName = tempYourObject.getName(); 
      arrItems.add(new SelectItem(tempId, tempName)); 
     } 
    } 
    return arrProfiles; 
} 
</code> 
関連する問題