저번에는 회원/멤버 관리 기능을 추가하였으니 이번에는 상품 관리 기능을 구현할 것이다.
상품 전체보기 - 상품 추가 기능 / 상품 목록이 나오도록 할 것이다.
[Java] ProductVo, ProductDao, ProductService
ProductVo package product; public class ProductVo { private int num; private String name; private int price; private int amount; public ProductVo() { } public ProductVo(int num, String name, int price, int amount) { super(); this.num = num; this.name = nam
skylarcoding.tistory.com
자바 파일은 위의 클래스 코드를 참고하면 된다.
index.jsp
회원관리 때 사용했던 index.jsp 파일에 아래 코드를 한줄 추가한다.
<a href = "/webApp2/product/list"> 상품 전체 목록</a>
List.java
서비스 객체를 생성해 getAll 서비스를 이용하고 ArrayList 변수에 담는다.
setAttribute 에 list 속성을 담고 list.jsp 로 이동한다.
package product.controller;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class SelectAll
*/
@WebServlet("/product/list")
public class List extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public List() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 제품 전체 검색 클릭
ProductService service = new ProductService();
ArrayList<ProductVo> list = service.getAll();
//db 프로덕트 전체 검색해서 list 에 담음
//request에 전체 결과 담음. -> 결과 뷰 페이지로 뿌려주는 것 list.jsp에서
request.setAttribute("list", list);
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
전체목록 기능에서 doPost는 필요하지 않아 doGet으로 이동하도록 한다.
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
list.jsp
list.jsp에서는 JSTL 방식을 이용해 자바 코드를 태그로 불러온다.
<c:forEach></c:forEach> 태그가 for 문 태그이다.
EL 표현식을 for 문 안에 넣어 리스트 내역을 모두 프린트하도록 한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 태그 라이브러리 사용하려면 태그 라이브러리 지시자를 사용해야한다. -->
<!-- 자바 코드를 태그로 만들어놓음 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>전체 상품 목록</h3>
<br />
<a href="/webApp2/product/add"> 상품추가</a>
<table border="1">
<tr>
<th>num</th>
<th>name</th>
<th>price</th>
<th>amount</th>
</tr>
<c:forEach var="vo" items="${list }">
<!-- vo 이름을 var로 지정. -->
<!-- ProductVo vo = list.get(i); -->
<tr>
<td>${vo.num }</td>
<td>${vo.name }</td>
<td>${vo.price }</td>
<td>${vo.amount }</td>
</tr>
</c:forEach>
</table>
</body>
</html>