FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot viewRoot = context.getViewRoot(); viewRoot.setViewId("/newpage.xhtml");
FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot viewRoot = context.getApplication().getViewHandler().createView(context, "/newpage.xhtml"); context.setViewRoot(viewRoot);In this example, we create a new UIViewRoot for a new view called newpage.xhtml using the createView method of the ViewHandler object obtained from the Application object. We set this new view root as the current view using the setViewRoot method of the FacesContext object. Both examples demonstrate how to use the setViewRoot method to change the view for the current request. This is useful when we need to programmatically change the view in response to user actions or business logic. The setViewRoot method is part of the javax.faces.context package.