[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