Java – create JSF view / component tree from XHTML file

I need to access the JSF page component tree when the application starts I found this source on the Internet

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

However, the generated viewroot does not have any child nodes Who knows the best way?

thank you.

Solution

You forgot to build the view You can use viewdeclarationlanguage #buildview() This is an excerpt from Javadoc (emphasizing my):

Therefore, this should be done:

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.

By the way, you can use viewdeclarationlanguage #createview() to create a view directly instead of the shorthand of 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.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>