我需要在应用程序启动时访问jsf页面组件树.我在网上找到了这个来源
UIViewRoot viewRoot = context.getApplication().getViewHandler().createView(context,"/path/to/some.xhtml");
但是生成的viewRoot没有任何子节点.
有谁知道最好的方法是什么?
谢谢.
解决方法
你忘了构建视图了.您可以使用
ViewDeclarationLanguage#buildView()
.这是
javadoc(强调我的)的摘录:
Take any actions specific to this VDL implementation to cause the argument
UIViewRoot
which must have been created via a call to 07002,to be populated with children.
因此,这应该做:
String viewId = "/path/to/some.xhtml"; FacesContext context = FacesContext.getCurrentInstance(); ViewHandler viewHandler = context.getApplication().getViewHandler(); UIViewRoot view = viewHandler.createView(context,viewId); viewHandler.getViewDeclarationLanguage(context,viewId).buildView(context,view); // view should now have children.
你可以顺便使用ViewDeclarationLanguage#createView()
直接创建视图而不是ViewHandler#createView()
的速记.
String viewId = "/path/to/some.xhtml"; FacesContext context = FacesContext.getCurrentInstance(); ViewDeclarationLanguage vdl = context.getApplication().getViewHandler().getViewDeclarationLanguage(context,viewId); UIViewRoot view = vdl.createView(context,viewId); vdl.buildView(context,view); // view should now have children.