[英]Pass Parameter to ViewScoped Bean
I'm going to pass a parameter from one page (Facelet) to a Managed Bean whose scope is View Scope. 我将参数从一页(Facelet)传递到范围为View Scope的Managed Bean。
I try to do it like this: 我尝试这样做:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class Mybean {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
First page: 第一页:
<h:body>
<h:form>
<h:commandLink value="click" action="index">
<f:setPropertyActionListener target="#{mybean.id}" value="20"/>
</h:commandLink>
</h:form>
</h:body>
second page: 第二页:
<h:body>
param value #{param.id}
<br />
bean value #{mybean.id}
<br />
<h:messages/>
</h:body>
But it does not show 20 却不显示20
@ViewScoped
bean stays only for the view that the user is watching. @ViewScoped
bean仅在用户正在观看的视图中停留。
Once the user switched to another view - the bean is being destroyed and created from scratch. 一旦用户切换到另一个视图-Bean将被破坏并从头开始创建。 Therefore, if you want to use the same bean for more than one page - use
@SessionScoped
bean. 因此,如果要使用同一bean进行一页以上的
@SessionScoped
,请使用@SessionScoped
bean。
Another way, is to create a Singleton
class in Java, and one bean will update the value in this class, while the other bean will extract the value from it. 另一种方法是用Java创建
Singleton
类,一个bean将更新该类中的值,而另一个bean将从中提取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.