2011-12-14 27 views
11

Hãy tưởng tượng trang JSF có một số thành phần như <h:selectOneMenu>, <h:dataTable>, <h:panelGrid>, v.v. Mỗi thành phần có một ID. Có phương pháp hay kỹ thuật nào mà tôi có thể lấy các thành phần lập trình khi hàm tạo của bean được gọi không?Lập trình UIC thành phần của khung nhìn JSF trong hàm tạo của bean

+4

"thực dụng" có một ý nghĩa hoàn toàn khác với "chương trình ". Vui lòng tham khảo một từ điển nếu bạn không chắc chắn về chính tả :) – BalusC

Trả lời

34

Bạn có thể lấy cây thành phần bởi FacesContext#getViewRoot() và tìm thấy một thành phần đặc biệt bởi ID UIComponentBase#findComponent():

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

Tuy nhiên, gốc xem là không trực tiếp có sẵn trong constructor của đậu cho mỗi gia nhập. Nó có thể trả lại null về các yêu cầu GET cho chế độ xem trong đó bean không được tham chiếu trong thẻ hoặc thuộc tính thời gian tạo chế độ xem. Tuy nhiên, nó được đảm bảo khả dụng trong sự kiện xem trước hiển thị.

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

với

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

Không liên quan cho vấn đề cụ thể, nó không phải là rõ ràng lý do tại sao bạn cần phải làm điều này, nhưng tôi có thể nói rằng đây là hơn thường là một mùi mã. Tôi đề nghị để điều tra nếu điều này thực sự là giải pháp đúng cho các yêu cầu chức năng cụ thể bạn đã có trong tâm trí. Để tìm manh mối và gợi ý, xem thêm 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

Mã này hoạt động cho tôi. Nhưng bạn phải chỉ định đường dẫn đầy đủ đến thành phần ví dụ "form: component1: someId" – Mindaugas

+0

@Mindaugas: đó là chính xác nếu chúng lần lượt nằm trong 'NamingContainer'. Xem thêm liên kết đến javadoc của 'findComponent()' trong câu trả lời của tôi. – BalusC

+0

@BalusC, "không liên quan" của bạn có giá trị nhất ở đây! – Maxym

3

Tôi đã thử giải pháp của bạn và nó đã hoạt động :). Ở đây tôi chỉ đăng câu trả lời của tôi rằng làm thế nào tôi đã làm nó. Trên thực tế, ai đó chỉ hỏi tôi câu hỏi này mà chúng tôi có thể nhận được tất cả các thành phần bên trong một nhà xây dựng khi trang của chúng tôi gọi, tức là tại sao tôi hỏi trên diễn đàn này :). Đây là mã của tôi

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

Tôi muốn hỏi rằng tôi có biểu mẫu (HtmlForm) ở đây, nhưng panel1 và myTable null, tại sao? Mặc dù tôi nhận được bảng điều khiển và bảng bằng cách kiểm tra trẻ em HtmlForm, nhưng tại sao tôi không thể nhận được chúng trực tiếp.

Cảm ơn

0

Nếu bảng điều khiển và bảng là về hình thức, hãy thử như sau:

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

thấy điều này 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; 
} 
Các vấn đề liên quan