[OmniFaces utilities] The
buildView()
method creates and builds a local view for the given view ID independently from the current view.Method:
In order to
programmatically create a view, the Components#buildView() utility method
follows these steps:
- Normalize the given view ID path as a valid view ID based on the current mapping, if necessary. If the current mapping is a prefix mapping and the given path starts with it, then remove it. If the current mapping is a suffix mapping and the given path ends with it, then replace it with the default. See:Faces#normalizeViewId().
- Use the current FacesContext and the given view ID to create an UIViewRoot via the current view handler (ViewHandler#createView()).
- Replace the current FacesContext with a custom implementation named, TemporaryViewFacesContext. This is capable to return the given (temporary) view in the getViewRoot() method and its associated renderer in the getRenderKit() method. Obviously, OmniFaces needs to instruct JSF to populate with children the new view using the associated render kit, but it canʹt simply invoke FacesContext#setViewRoot(new_view), because it canʹt be cleared afterwards ‐ the setViewRoot() doesnʹt accept a null being set. Practically, during the view build, OmniFaces sets the TemporaryViewFacesContext as the current FacesContext, and JSF will use the artifacts returned by TemporaryViewFacesContext#getViewRoot() and TemporaryViewFacesContext#getRenderKit().
- Populate the new view with children via, ViewDeclarationLanguage#buildView().
- Finally, restore the original FacesContext and return the new created view.
Note As a JSF developer, you may want to use this opportunity to explore more of OmniFaces source code. You have to admit that you don't see every day a real case example that replaces the default FacesContext. OmniFaces source code hides a lot of such examples that will really boost your JSF programming skills.
Whenever you
need to programmatically create a local view (UIViewRoot) independently
from the current view just pass the view ID to the Components#buildView() utility
method, as in the below example where we build the view for the foo.xhtml
page located in web app root (check out good and bad usages):
import
org.omnifaces.util.Components;
...
*** GOOD
USAGES ***
// just pass
the view name
UIViewRoot anotherView =
Components.buildView("foo.xhtml");
// leading
'/' is ok
UIViewRoot anotherView =
Components.buildView("/foo.xhtml");
// prefix
mapping is supported (e.g. /faces)
UIViewRoot anotherView =
Components.buildView("/faces/foo.xhtml");
*** BAD
USAGES ***
// skip view
extension
UIViewRoot anotherView =
Components.buildView("foo");
Output
error: viewId:/foo - View /foo could not
be restored.
// skip
leading '/' with prefix mapping
UIViewRoot anotherView =
Components.buildView("faces/foo.xhtml");
Output
error: java.io.FileNotFoundException:
...\faces\foo.xhtml (The system cannot find the path specified)
// point
inexistent view (buzz.xhtml doesn't exist)
UIViewRoot anotherView =
Components.buildView("buzz.xhtml");
Output
error: java.io.FileNotFoundException:
...\buzz.xhtml (The system cannot find the file specified)
Now, let's
suppose that the foo.xhtml view is in a folder named /pages
under web app root. We will have:
UIViewRoot anotherView =
Components.buildView("/pages/foo.xhtml");
// or with
prefix mapping (e.g. /faces)
UIViewRoot anotherView =
Components.buildView("/faces/pages/foo.xhtml");
Niciun comentariu :
Trimiteți un comentariu