Transfer objects between methods in a bean

Asked

Viewed 323 times

2

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

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

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

Only when I call the deleteUse function the user(private bean variable) 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;
    }


}
  • You have confirmed that the user1 of the parameter is not arriving null ?

  • It is arriving not null yes because when the method is triggered my Growl is updated with the name of the user in question.

  • Go debugging and see if you can get the time when the user gets null.

  • It seems that when I call the delete methodUse automatically my objects are reset, because by the end of the selection statement (selectUsuario) it keeps the allocated values.

  • 1

    I may be talking nonsense but do a test....

1 answer

2


With the help of my friend Douglas I managed to solve the problem.

He suggested that I change my scope to @Viewscoped, but 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.

  • 1

    Yes. The request scope ends when the server response is sent. That way all attributes die.

  • I’m glad you decided :)

Browser other questions tagged

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