我注意到有不同的bean范围,如:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
Run Code Online (Sandbox Code Playgroud)
每个人的目的是什么?如何为我的bean选择合适的范围?
根据我的理解,面部请求生命周期中放置在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.但是,它什么也没显示.
请纠正我在这个例子中对闪存范围的误解.非常感谢
我有一个JSF facelets页面,根据他们正在查看的页面显示数据表.当我显示第1页时,我调用view()action方法从数据库获取两个页面的数据,并将其存储为bean的私有成员字段(两个数组).我还调用conversation.start()了view()方法中注入的会话实例.
当用户单击"下一步"按钮(h:commandButton)转到第2页时,我正在执行一个next()方法来更新支持bean以指向数组2,以便打印出其内容.问题是,阵列2不再存在.我不知道为什么我会失去对话范围.有任何想法吗?
//tells the object which page we are on, and thus what data to display.
private int part = 1;
// These arrays are filled with data but conversation scope doesn't
// keep them on the next postback.
private int[] part1 = new int[15], part2 = new int[15];
Run Code Online (Sandbox Code Playgroud)