想象一下包含多個組件的JSF頁面,如<h:selectOneMenu>
,<h:dataTable>
,<h:panelGrid>
等。每個組件都有一個ID。有沒有什麼方法或技術可以在調用bean的構造函數時以編程方式獲取組件?以編程方式在bean的構造函數中獲取JSF視圖的UIComponent
回答
您可以通過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)。
此代碼適用於我。但是你必須指定組件的完整路徑,例如「form:component1:someId」 – Mindaugas 2013-07-19 08:47:12
我試過你的解決方案,它工作:)。在這裏,我只是發佈我的答案,我是如何做到的。其實有人問我這個問題,我們可以在我們的頁面調用時獲得構造函數中的所有組件,也就是爲什麼我在這個論壇上要求:)。這裏是我的代碼
/** 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子項來獲得面板和表格,但爲什麼我無法直接獲取它們。
感謝
如果面板和表在形式上,請嘗試以下操作:
UIComponent component2 = viewRoot.findComponent("exporterProfileUpdateRequestGrid").findComponent("panel1");
看到這個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;
}
- 1. JSF - 以編程方式獲取UIComponent的HTML
- 2. 在JSF託管bean的構造函數中訪問會話bean數據
- 3. 如何在構造函數中獲取矩形的視圖
- 4. asp.net MVC在視圖中以編程方式獲取HTML
- 5. 以編程方式在adf視圖對象中獲取行
- 6. 獲取自定義視圖構造函數中的CoordinatorLayout行爲
- 7. 以編程方式獲取視圖控制器的實例
- 8. 如何將UIComponent動態添加到託管bean的jsf視圖中?
- 9. 我是否以編程方式在ViewDidAppear,ViewDidLoad,ViewWillAppear,構造函數中添加SubViews?
- 10. 編程語言中的構造函數
- 11. iOS構建視圖以編程方式取決於對象ivar
- 12. 如何獲取調用託管bean的getter方法的UIComponent?
- 13. 構造函數Java編程
- 14. 多次調用JSF Backing Bean構造函數
- 15. JSF 2.0:添加UIComponent當編程
- 16. 獲取類的構造函數參數
- 17. 以編程方式創建觸發器時重載的構造函數錯誤
- 18. 將Scanner從構造函數中獲取到類中的方法?
- 19. 如何以編程方式獲取jsf或primeface數據表的所有行
- 20. 以編程方式在視圖控制器中訪問視圖
- 21. 以編程方式從MP4中獲取不重疊的圖像
- 22. 我如何在Sharepoint 2010中以編程方式獲取視圖列表
- 23. 以任何方式編程獲取視頻的FPS?
- 24. 的getAttributes()獲得( 「的attributeName」)在UIComponent構造返回null
- 25. 如何在__setattr__函數中獲取構造函數參數?
- 26. 在JSF bean中獲取請求URL?
- 27. 如何以編程方式獲取Windows進程的線程數?
- 28. 以編程方式在Access/SQL Server中導出視圖數據
- 29. 在對象的構造函數編程方式定義JavaScript屬性
- 30. 以編程方式檢索Bean
「務實」 有着完全不同的含義比「計劃」。如果您不確定拼寫,請參考字典:) – BalusC 2011-12-14 18:21:41