从xhtml文件创建jsf视图/组件树 [英] creating jsf view/Component tree from the xhtml file
问题描述
我需要在应用程序启动时访问jsf页面组件树.我在网上找到了这个来源
I need to access jsf pages component tree on application start up. I found this source on the net
UIViewRoot viewRoot = context.getApplication().getViewHandler().createView(context, "/path/to/some.xhtml");
,但生成的viewRoot没有任何子代. 有人知道什么是最好的方法吗?
but the resulting viewRoot doesn't have any children. Does anybody know what is the best way to do it?
谢谢.
推荐答案
您忘记了构建视图.您可以使用 javadoc (重点是我的):
You forgot to build the view. You can use ViewDeclarationLanguage#buildView()
for this. Here's an extract of its javadoc (emphasis mine):
Take any actions specific to this VDL implementation to cause the argument
UIViewRoot
which must have been created via a call tocreateView(javax.faces.context.FacesContext, java.lang.String)
, 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.
您还可以使用 ViewHandler#createView()
速记.
You can by the way also use the ViewDeclarationLanguage#createView()
directly to create the view instead of the ViewHandler#createView()
shorthand.
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.
这篇关于从xhtml文件创建jsf视图/组件树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!