【问题标题】:creating jsf view/Component tree from the xhtml file从 xhtml 文件创建 jsf 视图/组件树
【发布时间】:2012-08-24 14:38:07
【问题描述】:

我需要在应用程序启动时访问 jsf 页面组件树。我在网上找到了这个来源

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

但生成的 viewRoot 没有任何子级。 有谁知道最好的方法是什么?

谢谢。

【问题讨论】:

    标签: java jsf-2


    【解决方案1】:

    您忘记构建视图。您可以为此使用ViewDeclarationLanguage#buildView()。这是其javadoc 的摘录(重点是我的):

    采取任何特定于此 VDL 实现的操作,以使必须通过调用 createView(javax.faces.context.FacesContext, java.lang.String) 创建的参数 UIViewRoot填充子代

    因此,应该这样做:

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      • 2017-10-10
      • 2012-05-31
      • 2020-06-10
      相关资源
      最近更新 更多