首页 > 解决方案 > 如何创建一个类来存储对象列表并在JSP中显示而不删除列表?

问题描述

晚安,我正在学习JAVA WEB + JSP + SERVLETS,由于我是新手,所以我有以下问题:

我正在使用MVC 架构来创建我的应用程序的模型、视图和控制器。

我的应用程序的工作方式如下:在同一路线中,我应该从购物清单中制作CRUD,为此我在两个不同的包中有两个类,它们是:

模型 - Products.java 服务 - Shopping.java

在 Products 类中,我使用产品的代码、名称和数量来实例化一个新项目,然后使用 Purchases 我用来存储这些产品的列表并能够在我的 JSP 中使用它......

问题是:

1)如何创建这些实例化对象的列表并执行以下代码的方法?

2)如何在我的 JSP 中显示此列表..

3)如何在不更新页面的情况下进行表单的POST,同时我的产品列表不被删除?

4)如何将其他包中的类导入到我的 servlet 中?

Compras.java

   public class Compras {
    private List<Object> listaProdutos = new List<Object>();

    public Compras(List<Object> listaProdutos){
        this.listaProdutos = listaProdutos;
    }

    //Adds a new product to the List
    private addProduct(){}

    //Removes a specific product from the list
    private removeSpecificProduct(){}

    //Get all products from the list
    private getAllProducts(){}

    //Get a specific product from the list
    private getSpecificProduct(){}

    // Take the size of the list
    private getListSize(){}

    //Change the position of a specific product in the list
    private changeSpecificProductPosition(){}

}

产品.java

public class Produtos {
    private int quantity;
    private String name;

    public Produtos(int quantity, String name){
        this.quantity = quantity;
        this.name = name;
    } 
}

小服务程序 - Add.java

@WebServlet(name = "Add", urlPatterns = {"/Add"})
public class Add extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // REQUEST PRODUCT
        String productName = request.getParameter("productName").toUpperCase();
        int productQuantity = Integer.parseInt(request.getParameter("productQuantity"));

        // NEW PRODUCT
        Product newProduct = new Product(productName, productQuantity);

    }

}

JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <div>
            <h1><strong>Lista de Compras</strong></h1>
            <hr>
            <div></div>
        </div>
        <div>
            <h1>INCLUSÃO DE UM NOVO ITEM</h1>
            <form action="/add" method="POST" style="background-color: dimgray; padding: 10px;">
                <div style="padding: 5px">
                    <label style="color: white;"><strong>Item:</strong></label> 
                    <input name="productName" type="text" required></input>
                </div>
                <hr>
                <br>
                <div>
                    <label style="color: white;"><strong>Quantidade:</strong></label>
                    <input name="productQuantity" type="number" min="1" required></input
                </div>
                <hr>
                <div style="padding-top: 30px;">
                    <button type="submit">ADICIONAR</button>
                </div>
            </form>
        </div>
    </body>
</html>

标签: javajspservlets

解决方案


推荐阅读