Transfer objects between methods in a bean

2

I'm developing a web application using the framework bootsfaces in Java WEB

On one of my screens I have a datafile of bootsfaces that when one of the lines is clicked, is selected by activating the method selectUser.

This method receives a user1 object as a parameter and the user (private bean variable) receives user1.

But when I call the UserExclude function, the user (private variable in the bean) is null (empty).

How can I transfer objects between Methods?

This is my xhtml so far:

<h:form id="frmPrincipal">
    <div class="change" style="width: 1230px;">
        <b:dataTable value="#{beanUsuario.usuarios}" style="width: 100%; " id="tabelaUsuario"
                     page-length="5" page-length-menu="5,10,20,50,100" var="usuario"
                     widgetVar="usuario" customLangUrl="json/Portuguese-Brasil.json" 
                     select="true" selectionMode="single" 
                     onselect="ajax:beanUsuario.selecionaUsuario(usuario);"
                     >
            <b:dataTableColumn value="#{usuario.usuarioNome}" header-style="text-align:center;border-right:none;" 
                               content-style="border:none; text-align:center;" label="Nome"/>
            <b:dataTableColumn value="#{usuario.usuarioEmail}" header-style="text-align:center;" 
                               content-style="border:none; text-align:center;" label="Email"/>
            <b:dataTableColumn value="#{usuario.usuarioEnd}"  header-style="text-align:center;" 
                               content-style="border:none; text-align:center;" label="Endereço"/>
            <b:dataTableColumn value="#{usuario.usuarioTel}" header-style="text-align:center;" 
                               content-style="border:none; text-align:center;" label="Telefone"/>

        </b:dataTable>
    </div>

    <div style="float: right; margin-right: 20px;" class="produtosMain">
        <b:commandButton value="Excluir" icon="remove"  actionListener="#{beanUsuario.excluirUsuario()}" styleClass="produtosMainBt produtosMainBtExcluir"/>
    </div>
</h:form>

This is my Bean so far:

package controller;

import ejb.UsuarioFacadeLocal;
import model.Usuario;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

@Named(value = "beanUsuario")
@RequestScoped
public class BeanUsuario {


    private List<Usuario> usuarios;
    private Usuario usuario;

    @EJB
    UsuarioFacadeLocal usuarioEJB;

    @PostConstruct
    public void init() {
        usuarios = new ArrayList<Usuario>();
        usuario = new Usuario();

    }

   public void selecionaUsuario(Usuario usuario1) {
        RequestContext context = RequestContext.getCurrentInstance();//INSTANCIANDO VARIÁVEIS DE TELA
        FacesContext fContext = FacesContext.getCurrentInstance();

        usuario = usuario1;

        context.update("frmAltera");//ATUALIZANDO O FORM ALTERAR
        fContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Selecionado!", usuario.getUsuarioNome()));//ADICIONANDO MENSAGEM AO GROWL DA TELA
        context.update("frmPrincipal:message");

    }

    public void excluirUsuario() {
        System.out.println(usuario.getUsuarioNome());
        RequestContext context = RequestContext.getCurrentInstance();//INSTANCIANDO VARIÁVEIS DE TELA
        usuarioEJB.remove(usuario);
        context.update("frmPrincipal");

    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public List<Usuario> getUsuarios() {
        usuarios = usuarioEJB.findAll();
        return usuarios;
    }

    public void setUsuarios(ArrayList<Usuario> usuarios) {
        this.usuarios = usuarios;
    }

    public void setUsuarios(List<Usuario> usuarios) {
        this.usuarios = usuarios;
    }


}
    
asked by anonymous 30.08.2017 / 14:50

1 answer

2

With the help of my friend Douglas I solved the problem.

He suggested that I change my scope to @ViewScoped, but in this way the application did not access the Database.

It was then that I decided to change the scope to @SessionScoped and the problem was solved.

    
31.08.2017 / 15:57