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

sâmbătă, 28 martie 2015

[OmniFaces utililites (2.0)] Get original HTTP request URI (including query string) behind this forwarded request


[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestURI() method returns the original HTTP request URI behind this forwarded request, if any. This does not include the request query string.
[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestQueryString() method returns the original HTTP request query string behind this forwarded request, if any.
[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestURIWithQueryString() method returns the original HTTP request URI with query string behind this forwarded request, if any.

Method Faces#getForwardRequestURI()- returns the original HTTP request URI behind this forwarded request, if any

Method Faces#getForwardRequestQueryString() - returns the original HTTP request query string behind this forwarded request, if any

Method Faces#getForwardRequestURIWithQueryString() - returns the original HTTP request URI with query string behind this forwarded request, if any
Usage:

Let's suppose that we have a simple application named MyApp, and a simple scenario, based on the following items:

·         a JSF page (index.xhtml) that contains a <h:outputLink> that points to a Servlet, named ForwardServlet, and a simple form that contains a command button for navigating from page index.xhtml to a page named home.xhtml (this form is just for better highlighting the use of forward(request,response); effect):
...
<h:body>    
  <h:outputLink value="ForwardServlet?name=Rafael&amp;surname=Nadal">Go</h:outputLink>
       
  <h:form>              
    <h:commandButton value="Go" action="home"/>       
  </h:form>
</h:body>       
...

·         The ForwardServlet, which simply forwards the request/response to the home.xhtml page, via a RequestDispatcher#forward() call, as below:

@WebServlet(name = "ForwardServlet", urlPatterns = {"/ForwardServlet"})
public class ForwardServlet extends HttpServlet {
  ...
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    String nextPage = "/faces/home.xhtml";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextPage);
    dispatcher.forward(request, response);
  }
  ...
}

·         The home.xhtml page, which needs to check if the flow reaches there via a forward (e.g. passed through ForwardServlet) or not (e.g.  <h:form> submit), and for this it uses a simple test, as below:
...
<h:body>              
  <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.forward}</h:panelGroup>
  <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

The  ForwardBean uses the OmniFaces, Faces#getForwardRequestURI(), which will return null if you navigate to home.xhtml by submitting the form, and will return the forward request URI (without query string) if you navigate to home.xhtml via ForwardServlet:

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
   forward = Faces.getForwardRequestURI();
   return forward;
  }
}

The value of forward will be /MyApp/ForwardServlet.

If you need to obtain the forward request query string (without forward request URI), then just call Faces#getForwardRequestQueryString():
...
<h:body>              
 <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.queryString}</h:panelGroup>
 <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
    forward = Faces.getForwardRequestURI();
    return forward;
  }
   
  public String getQueryString(){
    return Faces.getForwardRequestQueryString();
  }
}

The value of queryString will be name=Rafael&surname=Nadal.

If you need both, the forward request URI and the forward request query string then simply invoke Faces#getForwardRequestURIWithQueryString():
...
<h:body>              
  <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.forwardAndQueryString}</h:panelGroup>
  <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
    forward = Faces.getForwardRequestURI();
    return forward;
  }
   
  public String getQueryString(){       
    return Faces.getForwardRequestQueryString();
  }
   
  public String getForwardAndQueryString(){
    forward = Faces.getForwardRequestURIWithQueryString();
    return forward;
  }
}

The value of forwardAndQueryString will be /MyApp/ForwardServlet?name=Rafael&surname=Nadal

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors