简体   繁体   中英

How does the POST REDIRECT GET works in JSF on commandButton

I was testing couple of new features of JSF and I came across Post Redirect Get. I wanted to redirect from my first page say first.xhtml to second.xhtml.

I have a number as a property in both the managed beans and I wanted to pass it to the second bean from the first bean using request parameter.

This is my first page

        <html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core">


        <head>
            <title>Landing Page</title>
        </head>

        <body>
        <h3>Enter Number</h3>
        <h:form>
            <h:inputText id="input" name="number" value="#{postRedirectGet.number}" />
            <h:commandButton value="redirect to result" 
    action="resultPage?faces-redirect=true&amp;includeViewParams=true">
            </h:commandButton>
        </h:form>
        </body>
        </html>

And in the second page I have

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
    <f:viewParam name="number" value="#{postRedirectResult.number}"/>
</f:metadata> 

<head>
    <title>Result Page</title>
</head>

<body>
<h:form>
    <h:outputText value="Number #{postRedirectGet.number}" />
    <h:outputText value="Number #{postRedirectResult.number}" />
    <h:commandButton value="Redirect to index" action="/index?faces-redirect=true" />
</h:form>
</body>
</html>

Now the page is doing a POST using commandButton and then redirecting to second page from first but it passes number=0 in the URL. It works if I change

<f:viewParam name="number" value="#{postRedirectResult.number}"/>

to

<f:viewParam name="number" value="#{postRedirectGet.number}"/>

but I thought the viewParam is used to set the value to a bean and not used to pass the values in URL. Could someone please explain how can we do POST and set the property of the managed bean on next page.

The problem is that the f:viewParam is used in two different ways in two scenarios . In h:link it is used to set the property of target bean , in h:commandButton it is used to compute the GET URL and then the target bean property can be set using @ManagedProperty . Is my understanding correct or can the f:viewParam be used to set the property in h:commandButton POST redirect get also.

What you seem to be missing is what includeViewParams does. Let me quote this very informative article (you should read all of it):

The other special query string parameter, includeViewParams, tells the navigation handler to include the view parameters when performing the navigation. But what view parameters should be included? The view parameters to be included when performing the navigation are declared on the to-view-id page.

So JSF looks at your resultpage.xhtml to determine which parameters to pass. And then dutifully proceeds to pass the current value of postRedirectResult#number (which at this time is unset/ 0 ).

To have the GET number parameter reflected in your bean, pass it as a real parameter:

<h:commandButton value="redirect to result" 
        action="resultPage?faces-redirect=true&amp;number=4" />

See also:

There are different 4 ways to transfer data from JSF Page To Backing Bean. We can use

  1. f:param
  2. f:setPropertyActionListener
  3. f:attribute
  4. Method expression (JSF 2.0).

Here you can try f:setPropertyActionListener as..

<h:commandButton value="redirect to result" 
        action="resultPage?faces-redirect=true">
    <f:setPropertyActionListener target="#{postRedirectResult.number}" value=4 />
</h:commandButton>

Here is the link for this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM