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

miercuri, 1 aprilie 2015

[OmniFaces utilities (2.1/2.0)] Send a temporary (302) redirect to the given URL


[OmniFaces utilities] The redirect() method sends a temporary (302) redirect to the given URL. If the given URL does not start with http://, https:// or /, then the request context path will be prepended, otherwise it will be the unmodified redirect URL. So, when redirecting to another page in the same web application, always specify the full path from the context root on (which in turn does not need to start with /). You can use String#format(String, Object...) placeholder %s in the redirect URL to represent placeholders for any request parameter values which needs to be URL-encoded. This method implicitly also calls Flash#setRedirect(boolean) with true so that any flash scoped attributes will survive the redirect.

Note The caller should preferably not catch the IOException thrown by this method,but just re-declare it in the action method.The Servlet container will handle it.

Method:
Usage:

Redirecting to external links

·         redirect to an external link without query string

import org.omnifaces.util.Faces;
...
// redirect to an external link without query string
Faces.redirect("http://showcase.omnifaces.org/");           

·         redirect to an external link with query string

import org.omnifaces.util.Faces;
...
// redirect to an external link with query string
Faces.redirect("http://showcase.omnifaces.org/components/param?exampleEntity=42");           

·         redirect to an external link with query string and String#format(String, Object...)

import org.omnifaces.util.Faces;
...
// redirect to an external link with query string and String#format(String, Object...)
Faces.redirect("http://showcase.omnifaces.org/components/param?exampleEntity=%s", String.valueOf(42));

Redirecting to another page in the same web application

Suppose that in Flash scope we have an attribute named, todayFeedback, representing the current date. You can set it via Faces#setFlashAttribute(), like this:

Faces.setFlashAttribute("todayFeedback", new java.util.Date());       

This attribute should "survive" to the next redirect, to feedback.xhtml page (keep in mind that Flash attributes "survives" by default to a single redirection, and they will be eliminated afterwards!). When we are using Faces#redirect(), OmniFaces will do that implicitly by invoking Flash#setRedirect(true) from Faces#redirect(). So, every time we use Faces#redirect(), Flash attributes will "survive" to current redirection.

·         JSF is prefix mapped (e.g. /faces/*) case

import org.omnifaces.util.Faces;
...
// JSF is prefix mapped (e.g. /faces/*)
Faces.redirect("faces/feedback.xhtml?email=" + email + "&info=" + info);
Faces.redirect("faces/feedback.xhtml?email=%s&info=%s", email, info);

·         JSF is suffix mapped (e.g. *.xhtml)

import org.omnifaces.util.Faces;
...
// JSF is suffix mapped (e.g. *.xhtml)
Faces.redirect("feedback.xhtml?email=" + email + "&info=" + info);
Faces.redirect("feedback.xhtml?email=%s&info=%s", email, info);

·         plain HTML page

import org.omnifaces.util.Faces;
...
// plain HTML page
Faces.redirect("foo.html?email=" + email + "&info=" + info);
Faces.redirect("foo.html?email=%s&info=%s", email, info);
__________________________________________________________________
Firebug reveals 302 status:

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