JSP, Servlet, MySQL/JSP - webmarket

액션 태그 : 상품 목록 표시하기 3

샤리미 2023. 9. 3. 06:00
728x90
반응형

chapter4-3 ※

06. 상품 목록 표시하기

jsp 페이지에 데이터를 보여주기 위한 자바 클래스인 자바빈즈를 작성하고 자바빈즈 액션 태그를 적용하여 웹 쇼핑몰의 상품 목록을 출력한다. 그리고 include 액션 태그를 적용하여 메뉴바와 본문, 바닥글 등으로 페이지를 모듈화 한다.

[상품목록 표시하기]

-상품 클래스 만들기

  1. 상품 클래스 생성하기 : /src/폴더에 dto 패키지를 생성하고 이 패키지에 Product 클래스를 생성한다.
  2. 멤버 변수 선언하기: 생성된 Product 클래스에 다음과 같이 멤버 변수를 작성한다.
package dto;

import java.io.Serializable;

public class Product implements Serializable {

	private static final long serialVersionUID = -4274700572038677000L;
	
	private String productId; //상품 아이디
	private String pname; //상품명
	private Integer uniPrice; //상품가격
	private String description; //상품 설명
	private String manufacturer; // 제조사
	private String category; //분류
	private long unitsInStock; //제고 수
	private String condition; //신상품 or 중고품 or 재생품
}

3.기본 생성자 작성하기: 기본 생성자와 선언된 멤버 변수인’상품 아이디’,’상품명’,’가격’을 매개변수로 하는 생성자를 추가 작성한다.

package dto;

import java.io.Serializable;

public class Product implements Serializable {

	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 unitsInStock; //제고 수
	private String condition; //신상품 or 중고품 or 재생품
	
	public Product() {
		super();
	}
	
	public Product(String productId, String pname, Integer unitPrice) {
		this.productId = productId;
		this.pname = pname;
		this.unitPrice = unitPrice;
	}
}

4.모든 멤버 변수의 Setter/Gertter()메소드 작성하기: 모든 멤버 변수의 Setter/Getter()메소드를 각각 추가 작성한다.

package dto;

import java.io.Serializable;

public class Product implements Serializable {

	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 unitsInStock; //제고 수
	private String condition; //신상품 or 중고품 or 재생품
	
	public Product() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Product(String productId, String pname, Integer unitPrice) {
	
		this.productId = productId;
		this.pname = pname;
		this.unitPrice = unitPrice;
		
		
	}
	
	public String getProductId() {
		return productId;
		
	}
	public String getPname() {
		return pname;
	}
	
	public void setPname(String pname) {
		this.pname = pname;
	}
	
	public void setProductId(String productId) {
		this.productId = productId;
	}
	
	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 getUnitsInStock() {
		return unitsInStock;
	}
	public void setUnitsInStock(long unitsInStock) {
		this.unitsInStock = unitsInStock;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
}

-자바빈즈로 사용할 상품 데이터 접근 클래스 만들기

5.자바빈즈로 사용할 클래스 만들기:/src/폴더에 dao 패키지를 생성하고 이 패키지에 ProductRepository클래스를 생성한다.

6.멤버 변수와 기본 생성자 만들기: 생성된 ProductRepository 클래스에 다음과 같이 작성한다.

package dto;

import java.util.ArrayList;

import dto.Product;

public class ProductiRepository {
	
	private ArrayList<Product> listOfProducts = new ArrayList<Product>();
	
	public ProductiRepository() {
		Product phone = new Product("P1234","iPhone 6s", 800000);
		phone.setDescription("4.7-inch, 1334X750 Renina HD display, 8-megapixel iSight Camera");
		phone.setCategory("Smart Phone");
		phone.setManufacturer("Apple");
		phone.setUnitsInStock(1000);
		phone.setCondition("New");
		
		Product notebook = new Product("P1235","LG PC 그램",150000);
		notebook.setDescription("13.3-inch, IPS LED display , 5rd Generation Intel Core processors");
		notebook.setCategory("Notebook");
		notebook.setManufacturer("LG");
		notebook.setUnitsInStock(1000);
		notebook.setCondition("Refurbishe");
		
		
		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.setUnitsInStock(1000);
		tablet.setCondition("Old");
		
		listOfProducts.add(phone);
		listOfProducts.add(notebook);
		listOfProducts.add(tablet);
		
		
	}
}

7.상품목록을 가져오는 메소드 만들기: 생성된 ProductRepository 클래스에 다음과 같이 추가한다.

package dto;

import java.util.ArrayList;

import dto.Product;

public class ProductiRepository {
	
	private ArrayList<Product> listOfProducts = new ArrayList<Product>();
	
	public ProductiRepository() {
		Product phone = new Product("P1234","iPhone 6s", 800000);
		phone.setDescription("4.7-inch, 1334X750 Renina HD display, 8-megapixel iSight Camera");
		phone.setCategory("Smart Phone");
		phone.setManufacturer("Apple");
		phone.setUnitsInStock(1000);
		phone.setCondition("New");
		
		Product notebook = new Product("P1235","LG PC 그램",150000);
		notebook.setDescription("13.3-inch, IPS LED display , 5rd Generation Intel Core processors");
		notebook.setCategory("Notebook");
		notebook.setManufacturer("LG");
		notebook.setUnitsInStock(1000);
		notebook.setCondition("Refurbishe");
		
		
		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.setUnitsInStock(1000);
		tablet.setCondition("Old");
		
		listOfProducts.add(phone);
		listOfProducts.add(notebook);
		listOfProducts.add(tablet);
		
		
	}
	
	public ArrayList<Product>getAllProducts(){  //객체 타이의 변수 listOfProducts
		return listOfProducts;            //에 저장된 모든 상품 목록을 가져오는
	}                                    //getAllProduct()메소드를 작성한다. 
}

-상품 목록 표시하기

 

8.상품 목록 출력 웹 페이지 마들기:products.jsp 파일을 생성하고 다음과 같이 작성한 후 웹 브러우저에 ‘http://localhost:8080/WebMarket/products.jsp’ 를 입력하여 실행한다.

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="dto.Product" %>
<jsp:useBean id="productDAO" class="dto.ProductiRepository" scope="session" />
<html>
	<head>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
		<title>상품목록</title>
	</head>
	<body>
		<jsp:include page="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>
					<p><%= product.getUnitPrice() %>원</p>
				</div>
				<%
					}
				%>
			</div>
			<hr>
		</div>
		<jsp:include page="footer.jsp"/>
	</body>
</html>

 

 

 

 

※자비빈즈 액션 태그의 기능과 사용법 요약※

  • 자바빈즈는 동적 콘텐츠 개발을 위해 자바 코드를 사용하여 자바 클래스로 로직을 작성하는 방법이다.
  • useBean 액션 태그로 자바빈즈 사용하기 : useBean 액션 태그는 JSP ㅠㅔ이지에서 자바빈즈를 사용하기 위해 실제 자바 클래스를 선언하고 초기화하는 태그이다.
  • setProperty 액션 태그로 프로퍼티의 값 저장하기 : setProperty 액션 태그는 useBean 액션 태그와 함께 자바빈즈의 Setter()메소드에 접근하여 자바빈즈의 멤버 변수인 프로퍼티의 값을 저장하는 태그입니다.
  • getPropetty 액션 태그로 프로퍼티의 값가져오기 : getProperty 액션 태그는 useBean 액션 태그와 함께 자바빈즈의 Getter()메소드에 접근하여 자바빈즈의 멤버 변수인 프로퍼티의 값을 가져오는 태그이다. 
728x90
반응형