2011-12-14 48 views
11

想象一下包含多個組件的JSF頁面,如<h:selectOneMenu>,<h:dataTable>,<h:panelGrid>等。每個組件都有一個ID。有沒有什麼方法或技術可以在調用bean的構造函數時以編程方式獲取組件?以編程方式在bean的構造函數中獲取JSF視圖的UIComponent

+4

「務實」 有着完全不同的含義比「計劃」。如果您不確定拼寫,請參考字典:) – BalusC 2011-12-14 18:21:41

回答

34

您可以通過FacesContext#getViewRoot()獲取組件樹和UIComponentBase#findComponent()找到ID特定組件:

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

然而,視圖根是不是在bean的構造本身直接提供。它可能會返回null上的GET請求到一個視圖,其中bean未在視圖構建時間標記或屬性中引用。但是保證在預渲染視圖事件期間可用。

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

public void 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 2013-07-19 08:47:12

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控件)在這裏,但PANEL1和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; 
} 
相關問題