04. 상품 목록 표시하기
2020. 9. 25. 17:28
728x90
Product.java
package DTO;
import java.io.Serializable;
public class Product implements Serializable{
public Product(String productId, String pname, Integer unitPrice) {
super();
this.productId = productId;
this.pname = pname;
this.unitPrice = unitPrice;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
private static final long serialVersionUID = -4274700572038677000L;
private String productId; //상품 아이디
private String pname; //상품명
private Integer unitPrice; //상품 가격
private String description; //상품 설명
private String manufacturer; //제조사
private String category; //분류
private long unitsInSock; //재고 수
private String condition; //신상품 or 중고품 or 재생품
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(Integer unitPrice) {
this.unitPrice = unitPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getUnitsInSock() {
return unitsInSock;
}
public void setUnitsInSock(long unitsInSock) {
this.unitsInSock = unitsInSock;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
}
- [Source] - [Generate Constructors from Superclass] - [Object] 선택 - [OK] - [Source] - [Generate Constructors Using Fields] - [productId, pname, unitPrice] 선택 - [OK] - [Source] - [Generate Getters and Setters] - [OK] |
ProductRepository.java
package DAO;
import java.util.ArrayList;
import DTO.Product;
public class ProductRepository {
private ArrayList<Product> listOfProducts = new ArrayList<Product>();
public ProductRepository() {
Product phone = new Product("P1234", "iPhone 6s", 800000);
phone.setDescription("4.17-inch, 1334X750 Renina HD display, 8-megapixel iSight Camera");
phone.setCategory("Smart Phone");
phone.setManufacturer("Apple");
phone.setUnitsInSock(1000);
phone.setCondition("New");
Product notebook = new Product("P1235", "LG PC 그램", 1500000);
notebook.setDescription("13.3-inch, IPS LED display, 5rd Generation Intel Core processors");
notebook.setCategory("NoteBook");
notebook.setManufacturer("LG");
notebook.setUnitsInSock(1000);
notebook.setCondition("Refurbished");
Product tablet = new Product("P1236", "Galaxy Tab S", 900000);
tablet.setDescription("212.8*125.6*6.6mm, Super AMOLED display, Octa-Core Processor");
tablet.setCategory("Tablet");
tablet.setManufacturer("Samsung");
tablet.setUnitsInSock(1000);
tablet.setCondition("Old");
listOfProducts.add(phone);
listOfProducts.add(notebook);
listOfProducts.add(tablet);
}
public ArrayList<Product> getAllProducts(){
return listOfProducts;
}
}
- 상품 목록을 저장하기 위한 ArrayList<Product> 객체 타입의 변수 listOfProducts 작성 - 기본 생성자를 만든 후 상품 정보를 설정하고 listOfProducts에 저장 - listOfProducts에 저장된 모든 상품 목록을 가져오는 getAllProducts() 메소드 작성 |
products.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="DTO.Product" %>
<jsp:useBean id="productDAO" class="DAO.ProductRepository" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 목록</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<%@ include file="Menu.jsp" %>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">
상품 목록
</h1>
</div>
</div>
<%
ArrayList<Product> listOfProducts = productDAO.getAllProducts();
%>
<div class="container">
<div class="row" align="center">
<%
for(int i=0; i<listOfProducts.size(); i++){
Product product = listOfProducts.get(i);
%>
<div class="col-md-4">
<h3><%=product.getPname() %></h3>
<p><%=product.getDescription() %>
<p><%=product.getUnitPrice() %>원
</div>
<%
}
%>
</div>
<!-- 구분선 -->
<hr>
</div>
<%@ include file="Footer.jsp" %>
</body>
</html>
- java.util.ArrayList 패키지 사용을 위한 import 속성 작성 - DTO.Product 패키지 사용을 위한 import 속성 작성 - 자바빈즈로 생성한 ProductRepository 클래스를 사용하도록 useBean 작성 - ProductReporitory 클래스의 getAllProducts() 메소드를 호출하여 반환된 결과 값을 listOfProduct에 저장 - lisfOfProduct에 저장된 상품 목록 개수만큼 실행하도록 반복 |
Products.jsp
728x90
반응형
'Code > Market' 카테고리의 다른 글
06. 상품 등록 페이지 만들기 (0) | 2020.09.29 |
---|---|
05. 상품 상세 정보 표시하기 (0) | 2020.09.25 |
03. 한글 출력 및 페이지 모듈화 (0) | 2020.09.25 |
02. 시작 페이지 만들기 (0) | 2020.09.25 |
01. 개발 환경 구축 (0) | 2020.09.25 |