Ken*_*han 30 jsf jsf-2 flash-scope
根据我的理解,面部请求生命周期中放置在Flash作用域内的对象将可用于下一个面部请求生命周期,然后清除.
假设我有两页:
page01.xhtml:
<h:form>
<h:commandButton action="#{page01Bean.action}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
Page01Bean:
@ManagedBean
@RequestScoped
public class Page01Bean {
public void action(){
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("fooKey", "fooValue");
}
}
Run Code Online (Sandbox Code Playgroud)
page02.xhtml:
<h:outputText value="#{flash.fooKey}"/>
Run Code Online (Sandbox Code Playgroud)
因此,当page01.xhtml
单击按钮时,面部请求生命周期(比如生命周期A)开始并将值设置为所调用的键下的闪存fooKey
然后我打开另一个浏览器选项卡并浏览 page02.xhtml
.另一个面临请求生命周期(比如生命周期B)开始呈现此页面.我预计生命周期B可以访问其上一个生命周期的闪存范围(即生命周期A)并显示fooValue
在其中 page02.xhtml
.但是,它什么也没显示.
请纠正我在这个例子中对闪存范围的误解.非常感谢
Fri*_*itz 33
简而言之,存储在闪存范围中的变量将在重定向后继续存在,之后将被丢弃.这在实现Post-Redirect-Get模式时非常有用.
如果您尝试通过重定向导航到另一个页面并在加载时访问属性,它们将在那里.完成该请求后,闪存中的值将被丢弃.例如:
你在page1.xhtml中,你有一个commandLink,它使用类似这样的方法重定向到一个新页面(注意:我将使用隐式导航).
public String navigateToPageB() {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("param1", "Hello World!");
return "pageB?faces-redirect=true";
}
Run Code Online (Sandbox Code Playgroud)
呈现pageB.xhtml时,您可以通过EL表达式访问这些值,例如
<h:outputLabel value="#{flash['param1']}" />
Run Code Online (Sandbox Code Playgroud)
这将显示"Hello World!" 我们之前在navigateToPageB中保存的字符串.
至于您的问题,通过在资源管理器中打开新选项卡,您无法访问上一个选项卡上访问的相同上下文,因此您的变量将不可用.
Xtr*_*ica 24
以前的答案是正确的,但为了完整性,我想说Mojarra实现中存在很多问题,但是最终他们实现了在Mojarra 2.1.27和2.2.5版本中正常工作.
正如@Gamb所说,闪存范围的目的是使参数保持活动状态,将其映射到重定向内部.如果需要,我们还可以将参数保持更长时间.除了上面提到的方法之外FacesContext#getCurrentInstance#getExternalContext#getFlash#put
,还有机会通过EL表达式设置参数,使用<c:set />
.我已经在SSCCE之后实施了一个基本测试,它使用两个视图显示了更广泛的选项:
Bean1
@ManagedBean
@ViewScoped
public class Bean1 implements Serializable {
/**
* Just takes the given param, sets it into flash context and redirects to
* page2
*
* @param inputValue
* @return
*/
public String goPage2(String inputValue) {
FacesContext.getCurrentInstance().getExternalContext().getFlash()
.put("param", inputValue);
return "page2?faces-redirect=true";
}
}
Run Code Online (Sandbox Code Playgroud)
page1.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head />
<h:body>
<!-- Sets the first flash param at the action method, which redirects to page 2 -->
<h:form>
<h:inputText value="#{inputValue}" />
<h:commandButton action="#{bean1.goPage2(inputValue)}"
value="Go Page 2" />
</h:form>
<!-- Sets the second flash param -->
<c:set target="#{flash}" property="param2" value="Myparam2" />
<!-- Tries to retrieve both of the params.
Note none of them is displayed at the first page hit.
If page refreshed, the second param which has been already set
using c:set above, will be displayed -->
<p>Param1: #{flash['param']}</p>
<p>Param2: #{flash['param2']}</p>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
Bean2
@ManagedBean
@ViewScoped
public class Bean2 implements Serializable {
public String getParam() {
/**
* Takes the parameter from the flash context
*/
return (String) FacesContext.getCurrentInstance().getExternalContext()
.getFlash().get("param");
}
}
Run Code Online (Sandbox Code Playgroud)
page2.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head />
<!-- This page just displays the received params -->
<h:body>
<!-- Different ways to retrieve the params from the flash scope -->
<p>Param1: #{bean2.param}</p>
<p>Param1: #{flash.param}</p>
<p>Param1: #{flash['param']}</p>
<p>Param2: #{flash['param2']}</p>
<!-- Keep the first param for next redirection -->
#{flash.keep.param}
<!-- Return to page1 and see how the first param is retained -->
<h:button outcome="page1?faces-redirect=true" value="return to 1" />
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
也可以看看:
归档时间: |
|
查看次数: |
37989 次 |
最近记录: |