[OmniFaces utilities] The
getRequestHeaderMap()
method returns the HTTP request header map.[OmniFaces utilities] The
getRequestHeader()
method returns the HTTP request header value associated with the given name.[OmniFaces utilities] The
getRequestHeaderValuesMap()
method returns the HTTP request header values map.[OmniFaces utilities] The
getRequestHeaderValues()
method returns the HTTP request header values associated with the given name.Method Faces#getRequestHeaderMap() - returns the HTTP request header map:
See also: Faces#getContext()
Method Faces#getRequestHeader() - returns the HTTP request header value associated with the given name:
See also: Faces#getContext()
Method Faces#getRequestHeaderValuesMap() - returns the HTTP request header values map:
Method Faces#getRequestHeaderValues() - returns the HTTP request header values associated with the given name:
See also: Faces#getContext()
Usage:
Let's
suppose a simple form, as below (the typed e-mail is rafa@rg.com; the typed
password is rafa2015):
<h:form
id="loginFormId">
E-mail: <h:inputText id="emailId"
required="true" value="#{loginBean.email}"/>
Password: <h:inputSecret
id="passwordId" required="true"
value="#{loginBean.password}"/>
<h:commandButton id="submitId"
value="Login" action="#{loginBean.loginAction()}">
<o:param name="name"
value="Rafael"/>
<o:param name="surname"
value="Nadal"/>
</h:commandButton>
</h:form>
The HTTP header
parameter map can be accessed via Faces#getRequestHeaderMap(), as below:
import
org.omnifaces.util.Faces;
...
Map<String, String>
requestHeaderMap = Faces.getRequestHeaderMap();
for
(Map.Entry<String, String> entry : requestHeaderMap.entrySet()) {
System.out.println(entry.getKey() +
"#" + entry.getValue());
// do something with header parameters
}
For example,
a possible output can be:
host#localhost:8080
user-agent#Mozilla/5.0
(Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0
accept#text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language#ro,ro_ro;q=0.8,en;q=0.7,en_us;q=0.5,fr;q=0.3,fr_fr;q=0.2
accept-encoding#gzip,
deflate
referer#http://localhost:8080/TestFaces/
cookie#JSESSIONID=081041b0fbc9018a0c4e26155a7d
connection#keep-alive
content-type#application/x-www-form-urlencoded
content-length#186
If you need
only the value of a single header parameter then you can use Faces#getRequestHeader().
For example, let's suppose that we are interested only in the accept
header parameter value. Then we write this:
import
org.omnifaces.util.Faces;
...
// the 'acceptValue'
will be 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
String acceptValue =
Faces.getRequestHeader("accept");
If you need
the "an immutable Map whose keys are the set of request header names
included in the current request, and whose values (of type String[]) are all of
the value for each header name returned by the underlying request. The returned
Map must implement the entire contract for an unmodifiable map as described in
the JavaDocs for java.util.Map. - Mojarra documentation", then you can use
Faces#getRequestHeaderValuesMap(),
as below:
import
org.omnifaces.util.Faces;
...
Map<String, String[]>
requestHeaderValuesMap = Faces.getRequestHeaderValuesMap();
for
(Map.Entry<String, String[]> entry : requestHeaderValuesMap.entrySet()) {
System.out.println(entry.getKey() +
"#" + Arrays.toString(entry.getValue()));
// do something with header parameters
}
For example,
a possible output can be:
host#[localhost:8080]
user-agent#[Mozilla/5.0
(Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0]
accept#[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
accept-language#[ro,ro_ro;q=0.8,en;q=0.7,en_us;q=0.5,fr;q=0.3,fr_fr;q=0.2]
accept-encoding#[gzip,
deflate]
referer#[http://localhost:8080/TestFaces/]
cookie#[JSESSIONID=082ed91c5c389e2bb6e0a317ec67]
connection#[keep-alive]
content-type#[application/x-www-form-urlencoded]
content-length#[186]
If you need
to returns the HTTP header parameter values associated with the given name,
then you can use Faces#getRequestHeaderValues(). Let's try the
same accept header parameter (now,
the values are placed in an array, String[]):
import
org.omnifaces.util.Faces;
...
// the 'acceptValues'
array will contain '[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]'
String[] acceptValues =
Faces.getRequestHeaderValues("accept");
Niciun comentariu :
Trimiteți un comentariu