0

My app is a traditional JSF 2.1 with facelets and acessing mySql 5.5 SGBD. So the problem is: I have a "managed-bean" shared by two jsf pages. A page for viewing and another for editing the person (JPA person class).

So if I use the "SessionScoped" annotation in my "MB", the person instance of the vision is shared with the editing. However, if I change the annotation to "viewScopped" of the MB, my "Person" instance is no longer shared.

How to get around this using the "viewScoped" annotation?

My code looks like this:

/**
* 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";
}
}

Thanks!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
BicaBicudo
  • 307
  • 1
  • 8
  • 20
  • possible duplicate of [JSF application correct use of session scope](http://stackoverflow.com/questions/8459903/jsf-application-correct-use-of-session-scope) – BalusC Feb 18 '13 at 16:49

1 Answers1

0

It's not possible to "share" the Person instance between two pages when you use @ViewScoped. That's because everytime you access a page that uses one bean annotated with @ViewScoped, the JSF will create a new instance of this bean.

So, if you want to use the same instance of Person between two pages, I guess you have two choices:

  1. Use SessionScoped as you are already using;

  2. Use a parameter to retrieve this Person on the MB. See JSF Passing parameter and redirecting to view scope

Community
  • 1
  • 1
Diogo Moreira
  • 1,082
  • 2
  • 9
  • 24