如何使视图参数在由@RequestScoped bean的视图中包含的表单引起的任何验证失败后仍然存在?

时间:2016-03-20 09:22:37

标签: jsf-2 parameter-passing mojarra viewparams

的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

enter image description here

将“For validation purpose”字段留空以触发验证失败

然后按下按钮。

现在在相同的字段中输入内容并再次按Enter键。

它工作得很好,视图参数就在那里。重定向的网址反映为:

  

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

当然,豆子是@ViewScoped

但是我的bean是@RequestScoped,这导致了问题。 我在验证失败后丢失了它们,然后在POST请求中删除了验证错误后发出了后续的POST,从而产生了这样的结果:

enter image description here

那么,问题归结为如何保留请求参数?

0 个答案:

没有答案