How to understand shopping cart logic in a JSF project?

1

I am developing a prototype of Ecommerce being that I am still trying to learn the logic of the shopping cart. By this link or this other of YouTube videos he does the following steps:

  • It creates a method that captures the record according to the line selected.

  • After this he update the tables via Ajax by address identifier (id) of the table that is the record to the table that will be added the new record applying the shopping cart logic, but noting that this way is only possible because the two tables are on the same page.

  • My goal is to at least capture the selected line and play the log to another page, but I can not figure out how to do this, I can even play the identifier address of the selected line using :param as in the code below, but I can not load the other page even though it has the address identifier.

    <p:button outcome="/noticias/CarrinhoCompras.xhtml"
        icon="ui-icon-cart " title="Carrinho">
            '
        <f:param name="noticia" value="#{noticia.id}" />
    </p:button>
    

    This was my last attempt by putting this snippet of code:

    <p:commandButton value="adicionar"
       action="#{carrinhoComprasBean.adicionar(noticia)}" />
    

    And this is my Bean class that lists the table:

    @Named
    @ViewScoped
    public class PesquisaNoticiasBean implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Inject
        private Noticias noticias;
    
        private NoticiaFilter filtro;
        private List<Noticia> noticiasFiltrados;
    
        private Noticia noticiaSelecionada;
    
        public PesquisaNoticiasBean() {
            filtro = new NoticiaFilter();
            noticiasFiltrados = new ArrayList<>();
        }
    
        public void pesquisar() {
    
            noticiasFiltrados = noticias.filtrados(filtro);
    
        }
    
        public void inicializar() {
            noticiasFiltrados = noticias.raizes();
        }
    
        public void excluir(){
            noticias.remover(noticiaSelecionada);
            noticiasFiltrados.remove(noticiaSelecionada);
            FacesUtil.addInfoMessage("Noticia " + noticiaSelecionada.getTitulo_noticia() + "excluída com sucesso");
        }
    
        public List<Noticia> getNoticiasFiltrados() {
            return noticiasFiltrados;
        }
    
        public NoticiaFilter getFiltro() {
            return filtro;
        }
    
        public Noticias getNoticias() {
            return noticias;
        }
    
        public Noticia getNoticiaSelecionada() {
            return noticiaSelecionada;
        }
    
        public void setNoticiaSelecionada(Noticia noticiaSelecionada) {
            this.noticiaSelecionada = noticiaSelecionada;
        }
    }
    

    And this is the Bean class that works by trying to transfer the selected record to the other table on another page:

    @Named
    @ViewScoped
    public class CarrinhoComprasBean implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Inject
        private Noticias noticias;
    
        private NoticiaFilter filtro;
        private List<Noticia> noticiasFiltrados;
    
        private List<Item> listaItens = new ArrayList<>();
    
        private Noticia noticiaSelecionada;
    
        public CarrinhoComprasBean() {
            filtro = new NoticiaFilter();
            noticiasFiltrados = new ArrayList<>();
        }
    
        public void pesquisar() {
    
            noticiasFiltrados = noticias.filtrados(filtro);
    
        }
    
    
        public String adicionar(Noticia noticia){
           Item item = new Item();
           item.setNoticia(noticia);
           item.setQuantidade(1);
           item.setValor(noticia.getPreco());
    
           //System.out.println("lista de itens " +item);
           listaItens.add(item);
           return "CarrinhoCompras";
        }
    
    //  public String addcart(Noticia n) {
    //      for (Item item : cart) {
    //          if (item.getNoticia().getId() == n.getId()) {
    //              item.setQuantidade(item.getQuantidade()+1);
    //              return "CarrinhoCompras";
    //          }
    //      }
    //      Item i = new Item();
    //      i.setQuantidade(1);
    //      i.setNoticia(n);
    //      cart.add(i);
    //      return "CarrinhoCompras";
    //  }
    
        public void inicializar() {
            noticiasFiltrados = noticias.raizes();
        }
    
        public void excluir() {
            noticias.remover(noticiaSelecionada);
            noticiasFiltrados.remove(noticiaSelecionada);
            FacesUtil.addInfoMessage("Noticia " + noticiaSelecionada.getTitulo_noticia() + "excluída com sucesso");
        }
    
        public List<Noticia> getNoticiasFiltrados() {
            return noticiasFiltrados;
        }
    
        public NoticiaFilter getFiltro() {
            return filtro;
        }
    
        public Noticias getNoticias() {
            return noticias;
        }
    
        public Noticia getNoticiaSelecionada() {
            return noticiaSelecionada;
        }
    
        public void setNoticiaSelecionada(Noticia noticiaSelecionada) {
            this.noticiaSelecionada = noticiaSelecionada;
        }
    
        public List<Item> getListaItens() {
            return listaItens;
        }
    
        public void setListaItens(List<Item> listaItens) {
            this.listaItens = listaItens;
        }
    }
    

    This is the method in question:

    public String adicionar(Noticia noticia){
        Item item = new Item();
        item.setNoticia(noticia);
        item.setQuantidade(1);
        item.setValor(noticia.getPreco());
    
        //System.out.println("lista de itens " +item);
        listaItens.add(item);
        return "CarrinhoCompras";
    }
    

    For those of you who want access to my complete code you are here my project on GitHub .

    I really need some help.

    Following the suggested suggestion, the method in the CartCompraBean class was modified;

    public String adicionar(Noticia noticia){
            Item item = new Item();
            item.setNoticia(noticia);
            item.setQuantidade(1);
            item.setValor(noticia.getPreco());
    
            // está aqui o código para imprimir
            System.out.println("lista de itens " +item);
    
    
    
            listaItens.add(item);
            return "CarrinhoCompras";
        }
    

    This was the message that came out on the eclipse consoles in the first list item in the dataTable

    lista de itens br.com.vendelancha.model.Item@6b10199b
    

    In the second item in the dataTable list, this

    lista de itens br.com.vendelancha.model.Item@53404bd1
    

    and that's how the add button was

                <p:commandButton value="adicionar"
                    action="#{carrinhoComprasBean.adicionar(noticia)}">
                    <f:setPropertyActionListener target="#{carrinhoComprasBean.noticiaSelecionada}"
                        value="#{noticia}" />
                </p:commandButton>
    

    When it was clicked, the facts described above happened and then it was directed to the other page, but it was not able to load the datable with the clicked item, since that is the purpose, to click on the item and this item is added in the other table in the another page.

    It's on this screen

    andwenttothisscreen,nothavingworkedasyoucansee.

        
    asked by anonymous 20.08.2015 / 16:17

    2 answers

    1

    One way you can send an object or a list of it to another page is like this:

    For example in your adicionar() method add the following code:

    List<Noticia> lista = new ArrayList<>();  
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("lista", lista);
    

    and to retrieve the list on your other page (or other bean):

    List<Noticia> pegaLista = (List<Noticia>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("lista");
    
        
    20.08.2015 / 20:58
    1

    For this your problem, you can continue sending the id by param, only in the other page you will have to do the following. Add the tag as it is next.

    <f:metadata>
        <f:viewParam name="noticia" converter="noticiaConverter"
                value="#{bean.noticia}"/> </f:metadata>
    

    You will need to create a converter that searches the news for the id passed as a parameter and returns the object to the news object of your bean.

        
    22.06.2016 / 13:49