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

Asked

Viewed 890 times

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

1 answer

0

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

Consultation 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> 

Landing 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>

Cadastrotarefamb

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

IMPORTANT

If you were to pass the entire object to the next page, surely you will have to load it completely on this page. Making consultation unnecessary and may influence system performance.

Source Code https://github.com/karanalpe/crud-adminfaces

Browser other questions tagged

You are not signed in. Login or sign up in order to post.