[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
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&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