微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/java/128690.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐