java – 从xhtml文件创建jsf视图/组件树

我需要在应用程序启动时访问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.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...