I have a 1: n relation, where a System can have multiple Loggers. I also have two beans: one for System (SystemBean that refers to the systems.xhtml page) and another for Logger (LoggerBean that refers to the loggers.xhtml page). In the sistema.xhtml page for each system displayed in a datatable, there is a button that when clicked, should go to the page loggers.xhtml and show all the loggers of that system. I do not know JSF very well, so what I do is the following (although I do not know if it's the right way to do it). When the user clicks on the system to see what their loggers are, I call a function that saves the system that was chosen in the session and then redirect the logging to the loggers.xhtml page. So:
public void vaiPraLoggers(Sistema sistema) {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getSessionMap().put("sistema", sistema);
NavigationHandler handler = context.getApplication().getNavigationHandler();
handler.handleNavigation(context, null,"/pages/alert/alert-loggers?faces-redirect=true" );
context.renderResponse();
}
So far, it's working fine, it's redirecting.
The problem is that on the loggers.xhtml page there is in addition to the datatable which displays all the system loggers a button to add a new logger. When this button is clicked, it should set as the system logger the system that was written to in the session that was loaded by the @PostConstructor of the LoggerBean. So:
@PostConstruct
private void initialize(Sistema sistema) {
FacesContext context = FacesContext.getCurrentInstance();
this.usuarioLogado = (Usuario) context.getExternalContext().getSessionMap().get("usuarioLogado");
this.sistema = (Sistema) context.getExternalContext().getSessionMap().get("sistema");
this.logger = new Logger();
this.loggerDao = new LoggerDAO();
And method that should save:
public void novoLogger() {
FacesContext context = FacesContext.getCurrentInstance();
msgTela = verificaRequisitosLogger();
if(StringUtils.isBlank(msgTela)) {
logger.setSistema(this.sistema);
this.loggerDao.salvar(logger, usuarioLogado);
this.listaLoggers.add(logger);
this.msgTela = Autenticador.getLocale().getString("sucessoLoggerSalvo");
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", msgTela));
}else {
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", msgTela));
}
}
What happens is that in the method that saves the logger, the system is null. It looks like it does not call @PostConstruct.
All my scopes are @ViewScoped, I do not know if this is what is interfering. Does anyone know what I'm doing wrong, or the best way to do what I want?
Thank you