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

Twitter

sâmbătă, 14 martie 2015

[OmniFaces utilities (2.0)] Normalize the given path as a valid view ID based on the current mapping


[OmniFaces utilities] The normalizeViewId() method normalize the given 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.

Method:
Usage:

The OmniFaces path normalization result depends on the faces servlet mapping. The getMapping() method determines and returns the faces servlet mapping used in the current request. If JSF is prefix mapped (e.g. /faces/*), then this returns the whole path, with a leading slash (e.g. /faces). If JSF is suffix mapped (e.g. *.xhtml), then this returns the whole extension (e.g. .xhtml). This is major, because normalizeViewId() treat these two cases separately:

·         If the current mapping is a prefix mapping and the given path starts with it, then remove it.

Let's consider this web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>   
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
   30
  </session-timeout>
 </session-config>
 <welcome-file-list>
  <welcome-file>faces/index.xhtml</welcome-file>
 </welcome-file-list>
</web-app>

This is a classic example of a faces servlet with prefix mapping. In this case, we can use normalizeViewId() method to normalize path of types /faces/page.xhtml, as below:

import org.omnifaces.util.Faces;
...
// return the normalized view ID: /home.xhtml
Faces.normalizeViewId("/faces/home.xhtml");

The normalized view ID will be /home.xhtml. The /faces will be removed.

·         If the current mapping is a suffix mapping and the given path ends with it, then replace it with the default Facelets suffix.

Let's consider this web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>
 <context-param>
  <param-name>javax.faces.FACELETS_SUFFIX</param-name>
  <param-value>.leo</param-value>
 </context-param>
<servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
   30
  </session-timeout>
 </session-config>
 <welcome-file-list>
  <welcome-file>index.xhtml</welcome-file>
 </welcome-file-list>
</web-app>

The javax.faces.FACELETS_SUFFIX  allow the web application to define an alternate suffix for Facelet based XHTML pages containing JSF content. If this init parameter is not specified, the default value is taken from the value of the constant DEFAULT_FACELETS_SUFFIX, which is, .xhtml. We modified as, .leo. Now, when we normalize a path as home.xhtml, the returned valid view ID will be home.leo:

import org.omnifaces.util.Faces;
...
// return the normalized view ID: home.leo
Faces.normalizeViewId("home.xhtml");

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors