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!