我需要在应用程序启动时访问jsf页面组件树.我在网上找到了这个来源
UIViewRoot viewRoot = context.getApplication().getViewHandler().createView(context, "/path/to/some.xhtml");
Run Code Online (Sandbox Code Playgroud)
但是生成的viewRoot没有任何子节点.有谁知道最好的方法是什么?
谢谢.
您忘记构建视图。您可以用于ViewDeclarationLanguage#buildView()此用途。这是其javadoc的摘录(重点是我的):
采取任何特定于此 VDL 实现的操作,使
UIViewRoot必须通过调用创建的createView(javax.faces.context.FacesContext, java.lang.String)参数填充子项。
因此,这应该这样做:
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.
Run Code Online (Sandbox Code Playgroud)
顺便说一下,您也可以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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1951 次 |
| 最近记录: |