1
I have a 1:n relationship, where a System can have multiple Loggers. I also have two Beans: one for System (Systemsbean that refers to page systems.xhtml) and another for Logger (Loggerbean that refers to page Loggers.xhtml). On the.xhtml systems page for each system displayed in a datatable, there is a button that when clicked, should go to the Loggers.xhtml page and show all the Loggers of that system. I don’t know very well JSF, so what I do is the following (although I don’t know if it’s the right way to do it). When the user clicks on the system to see which are his logger, I call a function that stores in the session the system that was chosen and then I redirect the nevegação to the page Loggers.xhtml. Thus:
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();
}
Until then, it’s working right, it’s redirecting.
The problem is that on the page Loggers.xhtml there is , in addition to the datatable that displays all the Loggers of the system a button to add a new logger. When this button is clicked, it should set as the logger system the system that was saved in the session that was loaded by the Loggerbean @Postconstructor. Thus:
@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 seems that it does not call @Postconstruct.
All my scopes are @Viewscoped, I don’t know if that’s what’s interfering. Someone knows what I’m doing wrong, or the best way to do what I want?
thank you