如何持久化在MB中创建并在两个jsf视图(页面)之间共享的实例类?

时间:2013-02-18 16:08:18

标签: jsf

我的应用程序是一个传统的JSF 2.1,带有facelets并且可以访问mySql 5.5 SGBD。所以问题是: 我有一个由两个jsf页面共享的“托管bean”。用于查看的页面和用于编辑人员的页面(JPA人员类)。

因此,如果我在“MB”中使用“SessionScoped”注释,那么视觉的人物实例将与编辑共享。但是,如果我将注释更改为MB的“viewScopped”,则不再共享我的“Person”实例。

如何使用“viewScoped”注释来解决这个问题?

我的代码如下所示:

/**
* Control layer should be session ou view scopped?????
*/
@ManagedBean
@SessionScoped
public class ControllerPessoa implements Serializable {
  private static final long serialVersionUID = 1L; 
  private Pessoa pessoa;
  private DataModel model;

  public ControllerPessoa() {
  // Necessary to view(non-edit)
  if (this.pessoa == null) {
    this.pessoa = new Pessoa();
  }
} 

@PostConstruct
public void init() {

}  

/** Used by the search routine */   
public List<Pessoa> getPessoaBy() {
  DaoPessoa dao = new DaoPessoa(Pessoa.class);
  List<Pessoa> listagem = null;
  try {
    listagem = new ArrayList<Pessoa>(); 

    switch (getCamposFiltro()) {
      // id
      case "1":
        if (dao.getPessoaById(pessoa.getIdPessoa()) != null) {
          listagem.add(this.pessoa);
        }
      break;
    model = new ListDataModel(listagem);
  } catch (Exception ex) {
    log.error("Erro na consulta às pessoas", ex);
  }
  return listagem;    
}

/**
* Here is the problem, in viewScopped mode the person instance isn't a safe state   instance
* @return "editCliente"
*/
public String alterar() {
  String retorno = null;
  try {
    this.pessoa = getPessoaFromEditOrDelete();
    setPessoa(this.pessoa);
    retorno = "editCliente";
  } catch (Exception ex) {
    log.error("Erro ao abrir a tela de edição de clientes", ex);
  }
  return retorno;
}

/**
* Retrieve the person instance from the dataTable
* @return the person instance
*/
private Pessoa getPessoaFromEditOrDelete() throws Exception {
  Pessoa p = (Pessoa)model.getRowData();
  return p;
}    


/**
* Make a new person instance to edit-view
* @return string to faces-config
*/
public String novo() {
  this.pessoa = new Pessoa();
  return "editCliente";
}
}

谢谢!

1 个答案:

答案 0 :(得分:0)

使用@ViewScoped时,无法在两个页面之间“共享”Person实例。那是因为每当你访问一个使用一个用@ViewScoped注释的bean的页面时,JSF就会创建一个这个bean的新实例。

所以,如果你想在两个页面之间使用相同的Person实例,我想你有两个选择:

  1. 正在使用SessionScoped;

  2. 使用参数在MB上检索此Person。请参阅JSF Passing parameter and redirecting to view scope