将参数传递给ViewScoped Bean [英] Pass Parameter to ViewScoped Bean

查看:90
本文介绍了将参数传递给ViewScoped Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将参数从一页(Facelet)传递给范围为View Scope的Managed Bean.

I'm going to pass a parameter from one page (Facelet) to a Managed Bean whose scope is View Scope.

我尝试这样做:

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;
  }    
}

第一页:

  <h:body>            
    <h:form>
      <h:commandLink value="click" action="index">
        <f:setPropertyActionListener target="#{mybean.id}" value="20"/>
      </h:commandLink>
    </h:form>
  </h:body>

第二页:

  <h:body>
    param value #{param.id}
    <br />
    bean value #{mybean.id}
    <br />

    <h:messages/>
  </h:body>

但它不显示20

推荐答案

@ViewScoped bean仅保留用于用户正在观看的视图.

@ViewScoped bean stays only for the view that the user is watching.

一旦用户切换到另一个视图-Bean将被销毁并从头开始创建. 因此,如果您想将同一个bean用于多个页面,请使用@SessionScoped bean.

Once the user switched to another view - the bean is being destroyed and created from scratch. Therefore, if you want to use the same bean for more than one page - use @SessionScoped bean.

另一种方法是用Java创建一个Singleton类,一个bean将更新该类中的值,而另一个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.

这篇关于将参数传递给ViewScoped Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆