luni, 23 martie 2015

[OmniFaces utilities (2.0)] Get the HTTP request context path


[OmniFaces utilities] The getRequestContextPath() method Returns the HTTP request context path. It's the webapp context name, with a leading slash. If the webapp runs on context root, then it returns an empty string.

Method:
See also: Faces#getContext()
Usage:

import org.omnifaces.util.Faces;
...
String requestContextPath = Faces.getRequestContextPath();

Note Do not confuse the  HTTP request context path (application context name) with the application name. Usually, the application context name is by default the application name, but this is not a must. For example you can have an application named foo and, in WildFly, you can specify the application context name in jboss-web.xml, as below:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="8.0" xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/schema/jbossas/jboss-web_8_0.xsd">
  <context-root>/foo_wildfly</context-root>
</jboss-web>

In GlassFish, the same thing can be accomplish in glassfish-web.xml, as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <context-root>/foo_glassfish</context-root>
</glassfish-web-app>

Now, the Faces#getRequestContextPath() will return the value of <context-root>, not /foo:

import org.omnifaces.util.Faces;
...
// for WildFly, 'requestContextPath' will be '/foo_wildfly'
String requestContextPath = Faces.getRequestContextPath();

// for GlassFish, 'requestContextPath' will be '/foo_glassfish'
String requestContextPath = Faces.getRequestContextPath();

Niciun comentariu:

Trimiteți un comentariu