7

アプリケーションの起動時にjsfページのコンポーネントツリーにアクセスする必要があります。私はネット上でこの情報源を見つけました

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

ただし、結果のviewRootには子がありません。誰かがそれを行うための最良の方法を知っていますか?

ありがとう。

4

1 に答える 1

4

ビューを作成するのを忘れました。これに使用できます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.

ちなみに、省略形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.
于 2012-08-30T02:37:56.327 に答える