[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:
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
· 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);
__________________________________________________________________
Niciun comentariu :
Trimiteți un comentariu