简体   繁体   English

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

[英]creating jsf view/Component tree from the xhtml file

I need to access jsf pages component tree on application start up. 我需要在应用程序启动时访问jsf页面组件树。 I found this source on the net 我在网上找到了这个来源

   UIViewRoot viewRoot = context.getApplication().getViewHandler().createView(context, "/path/to/some.xhtml");

but the resulting viewRoot doesn't have any children. 但是生成的viewRoot没有任何子节点。 Does anybody know what is the best way to do it? 有谁知道最好的方法是什么?

thanks. 谢谢。

You forgot to build the view. 你忘了构建视图了。 You can use ViewDeclarationLanguage#buildView() for this. 您可以使用ViewDeclarationLanguage#buildView() Here's an extract of its javadoc (emphasis mine): 这是它的javadoc (强调我的)的摘录:

Take any actions specific to this VDL implementation to cause the argument UIViewRoot which must have been created via a call to createView(javax.faces.context.FacesContext, java.lang.String) , to be populated with children . 执行特定于此VDL实现的任何操作,以导致必须通过调用createView(javax.faces.context.FacesContext, java.lang.String)创建的参数UIViewRoot以填充子项

Thus, this should do: 因此,这应该做:

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.

You can by the way also use the ViewDeclarationLanguage#createView() directly to create the view instead of the ViewHandler#createView() shorthand. 您可以顺便使用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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM