My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


vineri, 25 septembrie 2015

[OmniFaces utilities 2.2] Create and build a local view for the given view ID independently from the current view

[OmniFaces utilities] The buildView() method creates and builds a local view for the given view ID independently from the current view.


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;
// 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: ...\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: ...\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


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors