据我所知,放置在faces请求生命周期中的Flash作用域中的对象将可用于下一个faces请求生命周期,然后清除。
假设我有两页:
页面01.xhtml
<h:form>
<h:commandButton action="#{page01Bean.action}" />
</h:form>
Page01Bean :
@ManagedBean
@RequestScoped
public class Page01Bean {
public void action(){
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("fooKey", "fooValue");
}
}
页面02.xhtml
<h:outputText value="#{flash.fooKey}"/>
因此,当单击page01.xhtml
中的按钮时,一个faces请求生命周期(例如生命周期A)将启动并将该值设置为名为fooKey
的闪存。
然后打开另一个浏览器选项卡并浏览page02.xhtml
。另一个faces请求生命周期(例如生命周期B)开始呈现此页面。我希望生命周期B可以访问它以前的生命周期范围(即生命周期A),并在fooValue
中显示page02.xhtml
,但是它没有显示任何内容。
请纠正我对这个例子中闪光灯范围的误解。非常感谢
发布于 2012-06-25 17:40:43
简而言之,存储在闪存范围中的变量将在重定向中存活下来,它们随后将被丢弃。这在实现后重定向-获取模式时非常有用。
如果您试图通过重定向和访问加载中的属性来导航到另一个页面,它们就会出现。在完成该请求之后,闪存中的值将被丢弃。例如:
您在page1.xhtml中,并且有一个重定向到新页面的commandLink,这个方法类似于这个方法(注意:我将使用隐式导航)。
public String navigateToPageB() {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("param1", "Hello World!");
return "pageB?faces-redirect=true";
}
当呈现pageB.xhtml时,可以通过EL表达式访问这些值,如
<h:outputLabel value="#{flash['param1']}" />
它将显示“你好世界!”前面在navigateToPageB中保存的字符串。
至于您的问题,通过在资源管理器中打开一个新的选项卡,您将无法访问与上一个选项卡上访问的上下文相同的上下文,因此您的变量将不可用。
发布于 2014-01-22 08:21:19
前面的答案是正确的,但为了完整起见,我想说在Mojarra实现中使用了所有这些东西,但最终他们实现了在Mojarra 2.1.27和2.2.5版本中使其正常工作。
正如@Gamb所说,闪存作用域的目的是保持一个参数在内部通过重定向映射它。如果需要的话,我们也可以使参数保持更长的存活时间。除了前面提到的方法,FacesContext#getCurrentInstance#getExternalContext#getFlash#put
,还有通过EL表达式使用设置参数的机会。我在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";
}
}
页面1.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>
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");
}
}
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>
还请参见:
https://stackoverflow.com/questions/11194112
复制相似问题