0

index.xhtml:

    <f:metadata>
        <f:viewParam id="playernameparam" name="playernameparam" required="true" 
                requiredMessage="Player name required!" 
                value="#{playersBean.playerName}"/>            
        <f:viewParam id="playersurnameparam" name="playersurnameparam" required="true" 
                requiredMessage="Player surname required!" 
                value="#{playersBean.playerSurname}"/> 
    </f:metadata>
    <h:message for="playernameparam" />
    <h:message for="playersurnameparam" />
    <h:head>
        <title></title>
    </h:head>   
    <h:body>        
        <h:form>
            Enter name:<h:inputText value="#{playersBean.playerName}"/><br/>
            Enter surname:<h:inputText value="#{playersBean.playerSurname}"/><br/>
            For validation purpose: <h:inputText id="address" 
                    value="#{playersBean.address}" required="true" 
                    requiredMessage="Kindly fill your address location" /><br/>
            <h:message for="address" style="color:red" /><br/>
            <h:commandButton value="Submit" action="#{playersBean.action()}">
            </h:commandButton>
        </h:form>       
    </h:body>

result.xhtml:

    <f:metadata>
        <f:viewParam name="playernameparam" value="#{playersBean.playerName}"/>            
        <f:viewParam name="playersurnameparam" value="#{playersBean.playerSurname}"/> 
    </f:metadata>
    <h:head>
        <title></title>
    </h:head>   
    <h:body>        
        You requested name: <h:outputText value="#{playersBean.playerName}"/><br/>
        You requested surname: <h:outputText value="#{playersBean.playerSurname}"/>       
    </h:body>

ManagedBean:

@ViewScoped
@ManagedBean(name = "playersBean")
public class PlayersBean implements Serializable {

    private String playerName;
    private String playerSurname;
    private String address;
    // getters & setters

    public String action() {
        return "results?faces-redirect=true&includeViewParams=true";
    }
}

On hitting http://localhost:8080/Leonard/faces/ch2/index.xhtml?playernameparam=FARHAN&playersurnameparam=ANSARI

enter image description here

Leave the 'For validation purpose' field blank so as to trigger validation failure

and then hit press button.

Now enter something into the same field and press enter again.

It works perfectly fine and the view parameters are there & the redirected url reflects as:

http://localhost:8080/Leonard/faces/ch2/results.xhtml?playernameparam=FARHAN&playersurnameparam=ANSARI

Ofcourse it will as the bean is @ViewScoped.

But my bean is @RequestScoped, which is causing the problem. I lost them when there is a validation failure and then subsequent POST after removing that validation error with the POST request yielding this result:

enter image description here

So, the question boils down to on how retain the request parameters?

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • Is this helpful? http://stackoverflow.com/q/12314081 – BalusC Mar 20 '16 at 09:47
  • @BalusC: How can I be so stupid this afternoon? I was trying to replicate your point 5 for view param in this thread http://stackoverflow.com/questions/4888942/viewparam-vs-managedpropertyvalue-param-id. I embed the f:param for only one param even though there were two in my question. A silly mistake. – Farhan stands with Palestine Mar 20 '16 at 09:57
  • @BalusC: Nevertheless, request you to please elaborate on how the f:param survived even though they were absent in the URL after the validation error appeared? – Farhan stands with Palestine Mar 20 '16 at 10:07
  • They are sent via POST. – BalusC Mar 20 '16 at 10:09
  • @BalusC: Initially firing a GET request to http://localhost:8080/Leonard/faces/ch2/index.xhtml?playernameparam=FARHAN&playersurnameparam=ANSARI, the param were extracted from the URL. On pressing the commandbutton & entering nothing in the required field so as to trigger the validation so as to prevent form submit, the URl changed to http://localhost:8080/Leonard/faces/ch2/index.xhtml. It failed to POST the content as there were validation errors.So, the request was complete with the render phase showing me the message. So the f param must have survived for this POST request only. and not next – Farhan stands with Palestine Mar 20 '16 at 10:15
  • If you want to retain them via GET instead of POST, use `` instead of ``. – BalusC Mar 20 '16 at 10:16

0 Answers0