flash.keep & flash.setKeepMessages(true) 重定向
flash.keep & flash.setKeepMessages(true) on redirection
我正在看 Anghel Leonard 一书中的一个例子 Mastering Java Server Faces 2.2.
作者举例说明了如何在bean创建时为下一次重定向请求保留数据@RequestScoped
。
这是 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 & 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>
和支持 bean-
@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 具有相同的目的。
作者这里好像有点糊涂了
或者我完全错了&他们在这里确实有用。
作者选择的代码有点混乱,但从我的角度来看,它确实达到了预期的效果。
flash.setKeepMessages(true);
这一行告诉 JSF 您希望将 FacesMessage
保留在 flash 作用域中。这是进行重定向时的一项要求,因为涉及多个后续请求,否则消息会从一个请求到另一个请求而消失。该行是必要的。
pullValuesFromFlashAction(null);
该行没有什么特别之处,只是从 flash 范围中获取名字和姓氏。从 preRenderView
调用它以从 flash 范围加载 bean 数据是正确的,但是从操作方法调用它是 多余的 (除非你想做一些日志记录到查看参数设置是否正确)。无论如何,如果您是从 JSF 开始的,我鼓励您使用 JSF 2.2,它具有 preRenderView
方法的无参数替换,称为 viewAction
.
最后但并非最不重要的一点,我建议您阅读 an answer 我前段时间展示了一个处理 flash 示波器的示例,您可能会发现它很有趣。
另请参阅:
- JSF Keep Messages docs
- How to show faces message in the redirected page
我正在看 Anghel Leonard 一书中的一个例子 Mastering Java Server Faces 2.2.
作者举例说明了如何在bean创建时为下一次重定向请求保留数据@RequestScoped
。
这是 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 & 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>
和支持 bean-
@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 具有相同的目的。 作者这里好像有点糊涂了
或者我完全错了&他们在这里确实有用。
作者选择的代码有点混乱,但从我的角度来看,它确实达到了预期的效果。
flash.setKeepMessages(true);
这一行告诉 JSF 您希望将 FacesMessage
保留在 flash 作用域中。这是进行重定向时的一项要求,因为涉及多个后续请求,否则消息会从一个请求到另一个请求而消失。该行是必要的。
pullValuesFromFlashAction(null);
该行没有什么特别之处,只是从 flash 范围中获取名字和姓氏。从 preRenderView
调用它以从 flash 范围加载 bean 数据是正确的,但是从操作方法调用它是 多余的 (除非你想做一些日志记录到查看参数设置是否正确)。无论如何,如果您是从 JSF 开始的,我鼓励您使用 JSF 2.2,它具有 preRenderView
方法的无参数替换,称为 viewAction
.
最后但并非最不重要的一点,我建议您阅读 an answer 我前段时间展示了一个处理 flash 示波器的示例,您可能会发现它很有趣。
另请参阅:
- JSF Keep Messages docs
- How to show faces message in the redirected page