0

I'm trying to make an "add to cart" button, I'm using a primefaces spinner so the user can increment the quantity of the product, but I can't make to pass the value from the spinner to the variable quantity I have. The name, description and price of the product I'm getting them from the database. At first I also had a quantity column in my database, but I deleted and instead add a stock column. When I click the button to add into the cart all the values are correct except the quantity which is 0.

This is my JSF Page

<div>
                <c:forEach begin="0" end="0" var="productos" items="#{productoController.productos}">
                <b:image name="/images/tropical.jpg" style="width: 150px; margin-top: 15px;"/>
                <h:outputLabel value="#{productos.nombreProducto}" style="font-size: 20px; position: fixed; font-family: Muli;"></h:outputLabel>
                <h:outputLabel value="#{productos.descProducto}" style="font-size: 15px; text-align: end; font-family: Muli;"></h:outputLabel>
                <h:outputLabel value="#{productos.precioProducto} c/u" style="font-size: 18px; vertical-align: bottom; font-family: Muli; margin-left:180px;"></h:outputLabel>
                <p:spinner id="spinner" min="1" max="#{productos.stock}" value="#{productos.cantidad}"/>
                <b:commandButton value="Agregar al carrito"
                                 action="#{productoController.inserta(productos)}"
                                 look="success"
                                 style="width:60%"
                                  offset-md="4"
                                 />

                </c:forEach> 
            </div>

This is my Product Controller:

    public String inserta(Productos productos) {
    if (ProductoGestion.insertar(productos)) {
        return "bebidas.xhtml";
    } else {
        FacesMessage mensaje = new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Error",
                "Error al agregar al carrito");
        FacesContext.getCurrentInstance().addMessage(
                "Error",
                mensaje);
        return "login.xhtml";
    }
}

This is my Model:

public static boolean insertar(Productos productos) {
    try {
        FacesContext context = FacesContext.getCurrentInstance();
        PreparedStatement ps = Conexion.getConexion().prepareStatement(SQL_INSERT_PRODUCTO);
        float total = (productos.getCantidad() * productos.getPrecioProducto());
        ps.setInt(1, Math.round(total));
        ps.setInt(2, Math.round(productos.getPrecioProducto()));
        ps.setInt(3, productos.getCantidad());
        ps.setInt(4, productos.getId());
        ps.setString(5, String.valueOf(context.getExternalContext().getSessionMap().get("correo")));
        return (ps.executeUpdate() == 1);
    } catch (SQLException e) {
        ExceptionUtils.getStackTrace(e);
    }
    return false;
}

My Producto class:

  public Productos() {
}


private int id;
private String nombreProducto;
private String descProducto;
private float precioProducto;
private int stock;
private int cantidad;


public Productos(int id, String nombreProducto, String descProducto, float precioProducto, int stock) {
    this.id = id;
    this.nombreProducto = nombreProducto;
    this.descProducto = descProducto;
    this.precioProducto = precioProducto;
    this.stock = stock;
}

public int getCantidad() {
    return cantidad;
}

public void setCantidad(int cantidad) {
    this.cantidad = cantidad;
}

public int getStock() {
    return stock;
}

public void setStock(int stock) {
    this.stock = stock;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNombreProducto() {
    return nombreProducto;
}

public void setNombreProducto(String nombreProducto) {
    this.nombreProducto = nombreProducto;
}

public String getDescProducto() {
    return descProducto;
}

public void setDescProducto(String descProducto) {                                                                                        
    this.descProducto = descProducto;
}

public float getPrecioProducto() {
    return precioProducto;
}

public void setPrecioProducto(float precioProducto) {
    this.precioProducto = precioProducto;
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
geeeorge15
  • 43
  • 4
  • 2
    same way as plain `h:inputText`. And narrow the problem down. Remove the database code, create a [mcve] – Kukeltje Apr 15 '20 at 07:06
  • 1
    I had a deja-vu https://stackoverflow.com/questions/61202203/adding-items-into-cart-and-inserting-into-database why did you not improve the other quedtion and respond there? And why did you make the same error in thisuestion (all the database stuff)? – Kukeltje Apr 15 '20 at 07:12

0 Answers0