Pass Parameter to ViewScoped Bean

二次信任 提交于 2019-12-08 00:53:42

问题


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

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


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/4583968/pass-parameter-to-viewscoped-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!