[OmniFaces utilities] The
facesRedirect()
method sends a temporary (302) JSF redirect to the given URL, supporting JSF ajax requests. This does exactly the same as Faces#redirect(String, String...)
, but without the need for a FacesContext
. The major advantage is that you can perform the job inside a servlet filter or even a plain vanilla servlet, where the FacesContext
is normally not available. This method also recognizes JSF ajax requests which requires a special XML response in order to successfully perform the redirect.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.Note: Whenever an
IOException
points that something fails at I/O level the caller should preferably not catch it, but just redeclare it in the action method. The servlet container will handle it.Method:
· inside a servlet filter or even a plain vanilla servlet (basically, when FacesContext is not available):
import
org.omnifaces.util.Servlets;
...
@WebServlet("/MyServlet")
public class
MyServlet extends HttpServlet {
protected void
processRequest(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// place here the below redirects
examples
}
...
}
·
in JSF, when FacesContext is
available, Faces#redirect(), Faces#redirectPermanent(), or more
clumsy (not recommended):
import
org.omnifaces.util.Servlets;
...
FacesContext
context = FacesContext.getCurrentInstance();
ExternalContext
externalContext = context.getExternalContext();
HttpServletRequest
request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse
response = (HttpServletResponse) externalContext.getResponse();
// place here the below redirects examples
Faces.responseComplete();
// see also: Faces#responseComplete()
Examples:
Redirecting
to external links
·
redirect to an external link without query
string
// redirect to
an external link without query string
Servlets.facesRedirect(request, response,
"http://showcase.omnifaces.org/whatsnew");
·
redirect to an external link with query string
// redirect to
an external link with query string
Servlets.facesRedirect(request, response,
"http://showcase.omnifaces.org/components/param?exampleEntity=42");
// redirect
to an external link with query string and String#format(String, Object...)
Servlets.facesRedirect(request, response,
"http://showcase.omnifaces.org/components/param?exampleEntity=%s", String.valueOf(42));
Redirecting
to another page in the same web application
·
JSF is prefix mapped (e.g. /faces/*) case
// JSF is
prefix mapped (e.g. /faces/*)
Servlets.facesRedirect(request, response,
"faces/feedback.xhtml?email=" + email + "&info=" +
info);
Servlets.facesRedirect(request, response, "faces/feedback.xhtml?email=%s&info=%s", email, info);
·
JSF is suffix mapped (e.g. *.xhtml) case
// JSF is suffix
mapped (e.g. *.xhtml)
Servlets.facesRedirect(request, response, "feedback.xhtml?email="
+ email + "&info=" + info);
Servlets.facesRedirect(request, response, "feedback.xhtml?email=%s&info=%s",
email, info);
·
plain HTML page
// plain
HTML page
Servlets.facesRedirect(request, response, "foo.html?email="
+ email + "&info=" + info);
Servlets.facesRedirect(request, response, "foo.html?email=%s&info=%s",
email, info);
__________________________________________________________________Firebug reveals 302 status:
Note: In case of
JSF-AJAX request with query string use & instead
of simple, & (e.g. ?a=1&b=1 instead
of ?a=1&b=1)
Niciun comentariu :
Trimiteți un comentariu