晚安,我正在学习 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(){}
}
Produtos.java
public class Produtos {
private int quantity;
private String name;
public Produtos(int quantity, String name){
this.quantity = quantity;
this.name = name;
}
}
Servlets-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>