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 temporary (302) JSF redirect to the given URL, supporting JSF ajax requests


[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:
Usage:

·         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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors