2011-12-14 17 views
11

<h:selectOneMenu>, <h:dataTable>, <h:panelGrid> vb. Gibi çeşitli bileşenlerle birlikte bir JSF sayfası düşünün. Her bileşenin bir kimliği vardır. Fasulyenin kurucusu çağrıldığında bileşenleri program aracılığıyla alabilmem için herhangi bir yöntem veya teknik var mı?Fasulye yapıcısında bir JSF görünümünün UIComponents'larını programlı olarak alma

+4

"pragmatik" bkz programatik" den tamamen farklı bir anlama sahiptir ". Yazım hakkında emin değilseniz lütfen bir sözlüke danışın :) – BalusC

cevap

34

Sen FacesContext#getViewRoot() tarafından bileşen ağacı almak ve UIComponentBase#findComponent() tarafından kimliğe göre belirli bir bileşeni bulabilirsiniz:

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

Ancak, görünüm kök fasulyesi yapıcı başına doğrudan kullanılamaz. GET isteklerinde null, bakla görünüm görünüm etiketi veya özniteliğinde başvuruda bulunulmayan bir görünüme geri dönebilir. Ancak, pre render view olayı sırasında kullanılabilir olması garanti edilir. Bunu yapmak neden ihtiyaç somut soruna

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

İlgisiz ile

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

, açık değil, ama bu genellikle bir kod koku daha fazla olduğunu söyleyebilirim. Bunun akılda tutulan somut işlevsel gereksinim için doğru çözüm olup olmadığını araştırmayı öneririm. İpuçları ve ipuçları için ayrıca bkz. How does the 'binding' attribute work in JSF? When and how should it be used? ve How to use component binding in JSF right ? (request-scoped component in session scoped bean).

+1

Bu kod benim için çalışıyor. Ancak, örneğin "form: component1: someId" – Mindaugas

+0

@Mindaugas için tam yol belirtmeniz gerekir: bunlar bir 'NamingContainer' içinde olduklarında doğrudur. Ayrıca cevabımda 'findComponent()' nin javadoc bağlantısına da bakın. – BalusC

+0

@BalusC, "ilgisiz" burada en değerli! – Maxym

3

Çözümü denedim ve çalıştı :). Burada cevabımı sadece nasıl yaptığımı yazdım. Aslında birisi bana sorduğumuz soruyu sordu. Bu, sayfamızın ne zaman konuştuğunu, yani neden bu forumda sordum :). İşte

/** 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 

Ben burada formu (HtmlForm) var olduğunu sormak istiyorum benim kod olmakla panel1 ve myTable null, neden? Her ne kadar HtmlForm çocuklarını denetleyerek panel ve masa alsam da, neden onları doğrudan alamıyorum.

Teşekkür

0

paneli ve masa biçimindedir, aşağıdakileri deneyin:

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

bu 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; 
}