Best way for a bean to call another bean by passing itself as a parameter in JSF

1

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

    
asked by anonymous 08.12.2017 / 12:54

1 answer

0

In implementations using JSF I do not usually pass the entire object to the next page. What I usually do is pass the id to the destination page, and in this I do a query loading the object completely. For example:

Query page

                    <p:column headerText="Título"
                              filterBy="#{tarefa.titulo}" filterMatchMode="contains"
                              sortBy="#{tarefa.titulo}">
                        <h:link value="#{tarefa.titulo}" outcome="cadastro-tarefa.xhtml">
                            <f:param name="id" value="#{tarefa.id}"/>
                        </h:link>
                    </p:column> 

Destination page

<f:metadata>
    <f:viewParam name="id" value="#{cadastroTarefaMB.idTarefa}" converter="javax.faces.Long"/>
    <f:event listener="#{cadastroTarefaMB.inicializar()}" type="preRenderView"></f:event>
</f:metadata>

Registration MB Task

    public void inicializar() {
    if (idTarefa != null) {
        tarefa = tarefaService.porId(idTarefa);
    }
}

IMPORTANT

If you were to move the entire object to the next page, you will surely have to load it entirely on this page. Making an unnecessary query, which can influence system performance.

Source Code link

    
08.12.2017 / 14:52