2011-12-14 21 views
11

<h:selectOneMenu><h:dataTable><h:panelGrid>など、いくつかのコンポーネントを含むJSFページを想像してください。各コンポーネントにはIDがあります。 Beanのコンストラクタが呼び出されたときにコンポーネントをプログラムで取得する方法や手法はありますか?BeanのコンストラクタでJSFビューのUIComponentsをプログラムで取得する

+4

"実利" は、プログラム「よりも完全に異なる意味を持って見ます"スペルについてわからない場合は、辞書を参照してください:) – BalusC

答えて

34

あなたはFacesContext#getViewRoot()によってコンポーネントツリーを取得し、UIComponentBase#findComponent()によってIDによって特定のコンポーネントを見つけることができます。

UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); 
UIComponent component = viewRoot.findComponent("someId"); 
// ... 

しかし、ビューのルートは、それ自体がBeanのコンストラクタで直接使用できません。 Beanがビュー構築時のタグまたは属性で参照されていないビューに対して、GET要求のnullを戻すことがあります。ただし、プリレンダリングビューのイベント中に利用できることが保証されています。

public void init() { 
    // ... 
} 

無関係

<f:event type="preRenderView" listener="#{bean.init}" /> 

具体的な問題に、あなたがこれを行う必要があり、なぜそれが明確ではないのですが、私は、これは多くの場合、コードのにおいを超えていることを伝えることができます。私はこれが本当にあなたが念頭に置いていた具体的な機能要件のための正しい解決策であるかどうかを調査することを提案します。手がかりとヒントについては、How does the 'binding' attribute work in JSF? When and how should it be used?How to use component binding in JSF right ? (request-scoped component in session scoped bean)も参照してください。

+1

このコードは私のために働く。しかし、コンポーネントへのフルパスを指定する必要があります。例えば、 "form:component1:someId" – Mindaugas

+0

@Mindaugas:それらが順番に 'NamingContainer'内にある場合は正しいです。私の答えは 'findComponent()'のjavadocへのリンクも見てください。 – BalusC

+0

@BalusC、あなたの "無関係"はここで最も貴重です! – Maxym

3

私はあなたのソリューションを試してみました。ここで私はちょうど私がそれをしたという私の答えを投稿しています。実際に誰かがこの質問をしてくれました。ページが呼び出されたときにコンストラクタ内のすべてのコンポーネントを取得できます。つまり、なぜこのフォーラムで質問したのですか?ここに私のコードは

/** Creates a new instance of ExporterProfileUpdateRequestGrid */ 
public ExporterProfileUpdateRequestGrid() { 

    session = ConnectionUtil.getCurrentSession(); 
    exporterProfileUpdateRequestList = new ArrayList<ExporterProfileUpdateRequest>(); 

    int test = selectedRadioButton; 
    System.out.println(); 
    //getExporterProfileUpdateRequestGrid(); 

} //end of constructor 

@PostConstruct 
public void init() { 

    UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); 
    UIComponent component1 = viewRoot.findComponent("exporterProfileUpdateRequestGrid"); //form id 
    HtmlForm form = (HtmlForm)component1; 

    List<UIComponent> componentList = form.getChildren(); 

    for(int i=0; i<componentList.size(); i++) { 

     UIComponent component = componentList.get(i); 

     if (component instanceof Panel) { 

      Panel myPanel = (Panel)component; 

      List<UIComponent> panelComponentList = myPanel.getChildren(); 

      for(int j=0; j<panelComponentList.size(); j++) { 

       UIComponent panelComponent = panelComponentList.get(j); 

       System.out.println(); 


      } 

      System.out.println(); 

     } 

     System.out.println(); 

    } //end of for() 

    UIComponent component2 = viewRoot.findComponent("panel1"); //null 
    UIComponent component3 = viewRoot.findComponent("myTable"); // null 

} //end of init 

である私は、私はこちらのフォーム(HtmlForm)を得たことをお聞きしたいのですが、パネル1とmytableはヌル、なぜ?私はHtmlFormの子を調べることによってパネルとテーブルを取得しますが、なぜ私はそれらを直接取得できません。パネルやテーブルの形式である場合は、以下を試してみてください

おかげ

0

UIComponent component2 = viewRoot.findComponent("exporterProfileUpdateRequestGrid").findComponent("panel1"); 
2

このhttp://horrikhalid.com/2011/01/27/how-to-find-uicomponent-in-jsf/

public UIComponent findComponent(String id) { 

UIComponent result = null; 
UIComponent root = FacesContext.getCurrentInstance().getViewRoot(); 
if (root != null) { 
result = findComponent(root, id); 
} 
return result; 

} 

private UIComponent findComponent(UIComponent root, String id) { 

UIComponent result = null; 
if (root.getId().equals(id)) 
return root; 

for (UIComponent child : root.getChildren()) { 
if (child.getId().equals(id)) { 
result = child; 
break; 
} 
result = findComponent(child, id); 
if (result != null) 
break; 
} 
return result; 
}