flash.keep& flash.setKeepMessages(true)重定向

时间:2016-04-20 15:22:07

标签: jsf redirect jsf-2 messages flash-scope

我将在 Anghel Leonard 掌握Java Server Faces 2.2 中进行示例。

作者演示了如何在创建bean时为重定向保留下一个请求的数据@RequestScoped

enter image description here enter image description here

这是index.xhtml的代码 -

<h:body>
    <f:metadata> 
        <f:event type="preRenderView" 
            listener="#{playersBean.pullValuesFromFlashAction}"/> 
    </f:metadata>
    <h:messages />  
    <h:form>                       
        Name: <h:inputText value="#{playersBean.playerName}"/>
        Surname: <h:inputText value="#{playersBean.playerSurname}"/>
        <h:commandButton value="Register" 
            action="#{playersBean.addValuesToFlashAction()}"/>          
    </h:form>
</h:body>

terms.xhtml -

<h:body>
    <h:messages />  
    Hello, <h:outputText value="#{flash.keep.playerName} #{flash.keep.playerSurname}"/>    
    <br/><br/>Terms &amp; Conditions ... ... ... ... ...
    <h:form>
        <h:commandButton value="Reject" 
            action="#{playersBean.termsRejectedAction()}" />
        <h:commandButton value="Accept" 
            action="#{playersBean.termsAcceptedAction()}" />
    </h:form>
</h:body>

done.xhtml -

<h:head>
    <title></title>
</h:head>   
<h:body>
    <f:metadata> 
        <f:event type="preRenderView" 
            listener="#{playersBean.pullValuesFromFlashAction}"/> 
    </f:metadata>
    <h:messages />  
    <h:outputText value="#{playersBean.playerName} #{playersBean.playerSurname}"/> 
        successfully registered!           
</h:body>

支持豆 -

@ManagedBean
@RequestScoped
public class PlayersBean {

    private final static Logger logger = Logger.getLogger(PlayersBean.class.getName());
    private String playerName;
    private String playerSurname;
    public PlayersBean() {
    }
    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }
    public String getPlayerSurname() {
        return playerSurname;
    }
    public void setPlayerSurname(String playerSurname) {
        this.playerSurname = playerSurname;
    }
    public String addValuesToFlashAction() {

        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        flash.put("playerName", playerName);
        flash.put("playerSurname", playerSurname);
        return "terms?faces-redirect=true";
    }
    public void pullValuesFromFlashAction(ComponentSystemEvent e) {
        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        playerName = (String) flash.get("playerName");
        playerSurname = (String) flash.get("playerSurname");
    }
    public String termsAcceptedAction() {
        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

        flash.setKeepMessages(true);
        pullValuesFromFlashAction(null);

        //do something with firstName, lastName 
        logger.log(Level.INFO, "First name: {0}", playerName);
        logger.log(Level.INFO, "Last name: {0}", playerSurname);

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Terms accepted and player registered!"));
        return "done?faces-redirect=true";
    }
    public String termsRejectedAction() {
        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

        flash.setKeepMessages(true);
        pullValuesFromFlashAction(null);

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Terms rejected! Player not registered!"));
        return "index?faces-redirect=true";
    }
}

我认为不需要这两行 -

flash.setKeepMessages(true);
pullValuesFromFlashAction(null);

作为terms.xhtml页面上的flash.keep用于相同的目的。 作者似乎在这里感到困惑。

或者我完全错了&amp;他们确实在这里有目的。

1 个答案:

答案 0 :(得分:2)

作者选择的代码有点令人困惑,但是,从我的观点来看,它完成了它的目的。

  

flash.setKeepMessages(真);

这一行告诉JSF你想将FacesMessage保留在flash范围内。这是进行重定向时的一项要求,因为涉及多个后续请求,否则消息将从一个请求死亡到另一个请求。该行必要

  

pullValuesFromFlashAction(空);

该行没有什么特别之处,只需从闪存范围中获取名称和姓氏。从preRenderView调用它来从flash作用域加载bean数据是正确的,但它是冗余从动作方法中调用它(除非你想做一些日志记录来查看参数是正确设置)。无论如何,如果您从JSF开始,我建议您使用JSF 2.2,它具有preRenderView方法的无参数替换,称为viewAction

最后但并非最不重要的,我建议你通过an answer我前段时间展示一个处理闪存范围的例子,你可能会发现它很有趣。

另见: