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

duminică, 21 iunie 2015

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


[OmniFaces utilities] The redirectPermanent() method send a permanent (301) 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 This method does by design not work on AJAX requests. It is not possible to return a "permanent redirect" via JSF AJAX XML response.

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.redirectPermanent("http://showcase.omnifaces.org/whatsnew");

·         redirect to an external link with query string

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

import org.omnifaces.util.Faces;
...
// redirect to an external link with query string and String#format(String, Object...)
Faces.redirectPermanent("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#redirectPermanent(), OmniFaces will do that implicitly by invoking Flash#setRedirect(true) from Faces#redirectPermanent(). So, every time we use Faces#redirectPermanent(), 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.redirectPermanent("faces/feedback.xhtml?email=" + email + "&info=" + info);
Faces.redirectPermanent("faces/feedback.xhtml?email=%s&info=%s", email, info);

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

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

·         plain HTML page

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

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors