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
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:
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:
So, the question boils down to on how retain the request parameters?