[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:
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:
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);
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:
Firebug reveals 301 status:
Niciun comentariu :
Trimiteți un comentariu