How to understand shopping cart logic in a JSF project?

Asked

Viewed 1,849 times

1

I’m developing a prototype of Ecommerce being that I’m still trying to learn the logic of shopping cart. By that link or this other of videos on Youtube it makes the following steps:

  1. It creates a method that can capture the record according to the selected line.

  2. After that it update the tables via Ajax by the identifier address (id) of the table that is the record for the table that will be added to the new record applying shopping cart logic, but not forgetting to point out that this way is only possible because the two tables are on the same page.

My goal is to at least be able to capture the selected line and play the record to another page, but I can not imagine a way to do this, I can even play the identifier address of the selected line using :param as in the code below, however I cannot load the other page even having the identifier address.

<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 to put 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 trying to transfer the selected record to the other table on the other 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;
    }
}

That 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 who want to have access to my complete code is here my project on Github.

I really need help.

Following the proposed suggestion the method in the Carrinhocomprabean class has been 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 on the first list item in the datatable

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

In the second item of the datatable list came out this

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

and that’s how the add button

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

When it was clicked happened the facts described above and then was directed to the other page, but could not load the datable with the clicked item, because that’s the goal, click on the item and this item is added in the other table on the other page.

He’s on this screen

inserir a descrição da imagem aqui

and went to this screen, not having resulted as you can see.

inserir a descrição da imagem aqui

  • That button is inside a column?

  • positive, if you observe you will see that you are inside a column.

  • Observe where, because the code is not very clear rsrs. Anyway, try to use the <f:setPropertyActionListener> passing your selected object

  • I have tried it, but I did it without asking for help, but I will put it again as I have done and request that you may persist in helping me. soon I will update my post.

  • When update the post let me know that put a possible solution to your problem.

  • I just updated the post.

  • Already tried using the rowSelect <p:datatable event> ?

  • I don’t know, I’ll see what it’s about.

  • I just saw, I tried, but I didn’t succeed.

  • Ready, can you get the right object? you want to move this object to another page now?

  • Yes, of course I do

  • Come on part, your code is too big and still not clear. You want to pass an object or a list to the other page?

  • I want to do similar to video >>>>>> https://www.youtube.com/watch?v=IH5TeMObs2s

  • what will be the difference of passing the object or passing the list?

  • I cannot watch the video at the moment if you pass the Object is just one item, if you pass a list are several items

Show 10 more comments

2 answers

1

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

For example in your method adicionar() 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");
  • And will the pages stay the way they are? I just have to make these changes in the classes and that’s it?

  • Sorry, but this is not explained well. How do I put the code? where to put the code?

  • So I don’t know how your code is, but if you have a Bean for each page, the first snippet you can put in your method to redirect the page and when you redirect you can use an init() method with @Postconstruct notation(she will do the initial setup for your bean) and pick up the list with the second snippet.

  • It’s not possible to put a @Postconstruct because I’m using CDI in the project, but if I had seen my project on Github I wouldn’t have these mismatches.

1

For this your problem, you can continue sending the id by param, but on the other page you will have to do the following. Add the tag as follows.

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

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

Browser other questions tagged

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