的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";
}
}
点击http://localhost:8080/Leonard/faces/ch2/index.xhtml?playernameparam=FARHAN&playersurnameparam=ANSARI
将“For validation purpose”字段留空以触发验证失败
然后按下按钮。
现在在相同的字段中输入内容并再次按Enter键。
它工作得很好,视图参数就在那里。重定向的网址反映为:
当然,豆子是@ViewScoped
。
但是我的bean是@RequestScoped,这导致了问题。 我在验证失败后丢失了它们,然后在POST请求中删除了验证错误后发出了后续的POST,从而产生了这样的结果:
那么,问题归结为如何保留请求参数?