상품목록 페이지에 상품 추가 버튼 추가
products.jsp
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.List"%>
<%@page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>상품 목록</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script src="/js/jquery-3.6.0.js"></script>
<script>
$(function(){
$(document).on('click','.col-md-4',function(){
let id = $(this).attr("name");
location.href="/product.jsp?pid="+id;
})
})
</script>
</head>
<body>
<!-- header -->
<!--
디렉티브 태그 속성 속성의 값 -->
<%@ include file="menu.jsp" %>
<% // 스크립틀릿
// 클래스 객체 클래스 메서드(호출)
ProductRepository dao = ProductRepository.getInstance();
// 객체타입 변수 객체 메서드(호출)
List<ProductVO> list = dao.getAllProducts();
%>
<!-- 상품 목록 시작 -->
<div class="jumbotron">
<div class="container">
<h1 class="display-3">Product List</h1>
</div>
</div>
<div class="container">
<div class="row" align="center">
<c:set var="list" value="<%=list %>"/>
<!-- 상품 반복 부분 시작 -->
<!-- 객체 타입 변수 list에 저장된 상품 목록 개수만큼 실행하는 JSTL 반복문 -->
<c:forEach var="vo" items="${list}">
<div class="col-md-4" name="${vo.productId}">
<!-- EL 표현문 -->
<h3>${vo.pname}</h3>
<p>${vo.description}</p>
<p><fmt:formatNumber value="${vo.unitPrice}" type="number" pattern="#,###" />원</p>
<p>
<a href="product.jsp?pid=${vo.productId}" class="btn btn-secondary" role="button">상세정보 »</a>
</p>
</div>
</c:forEach>
<!-- 상품 반복 부분 끝 -->
</div>
<hr />
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<a href="/addProduct.jsp" class="btn btn-primary" role="button">등록</a>
</div>
</div>
</div>
<!-- 상품 목록 끝 -->
<!-- footer -->
<%@ include file="footer.jsp" %>
</body>
</html>
addProduct.jsp
상품 추가 입력 페이지(form페이지)
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.List"%>
<%@page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>상품 목록</title>
</head>
<body>
<!-- header -->
<!--
디렉티브 태그 속성 속성의 값 -->
<%@ include file="menu.jsp"%>
<!-- 상품 목록 시작 -->
<div class="jumbotron">
<div class="container">
<h1 class="display-3">상품 등록</h1>
</div>
</div>
<div class="container">
<form action="processAddProduct.jsp" method="post" class="form-horizontal" name="newProduct">
<div class="form-group row">
<label class="col-sm-2">상품 코드</label>
<div class="col-sm-3">
<input type="text" name="productId">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">상품명</label>
<div class="col-sm-3">
<input type="text" name="pname">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">가격</label>
<div class="col-sm-3">
<input type="text" name="unitPrice">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">상세정보</label>
<div class="col-sm-5">
<textarea rows="2" cols="23" name="description">
</textarea>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">제조사</label>
<div class="col-sm-3">
<input type="text" name="manufacturer">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">카테고리</label>
<div class="col-sm-3">
<input type="text" name="category">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">재고수</label>
<div class="col-sm-3">
<input type="text" name="unitsInStock">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">상태</label>
<div class="col-sm-5">
<input type="radio" id="new" name="condition" value="New">
<label for="new">신규 제품</label>
<input type="radio" id="old" name="condition" value="Old">
<label for="old">중고 제품</label>
<input type="radio" id="Refurbished" name="condition" value="Refurbished">
<label for="Refurbished">재생 제품</label>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-primary" type="submit" value="등록" />
</div>
</div>
<hr />
</form>
</div>
<!-- 상품 목록 끝 -->
<!-- footer -->
<%@ include file="footer.jsp"%>
</body>
</html>
processAddProduct.jsp
입력한 상품을 받아 처리하는 페이지
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
Enumeration name = request.getParameterNames();
ProductVO vo = new ProductVO();
while(name.hasMoreElements()){
// object 타입
// nextElement 메서드 : 각 요청 파라미터의 이름을 가져옴
String key = (String)name.nextElement();
String value = request.getParameter(key);
// if문으로 비교해서 넣기..
if(key.equals("productId")){
vo.setProductId(key);
} else if(key.equals("pname")){
vo.setPname(key);
} else if(key.equals("unitPrice")){
vo.setUnitPrice(Long.parseLong(value));
} else if(key.equals("description")){
vo.setDescription(value);
} else if(key.equals("manufacturer")){
vo.setManufacturer(value);
} else if(key.equals("category")){
vo.setCategory(value);
} else if(key.equals("unitsInStock")){
vo.setUnitsInStock(Integer.parseInt(value));
} else if(key.equals("condition")){
vo.setCondition(value);
}
}
out.print(vo);
%>
'JAVA > JSP' 카테고리의 다른 글
[JSP] 구현 4 - 이미지 추가 (0) | 2024.07.05 |
---|---|
[JSP] 파일 업로드 (0) | 2024.07.04 |
[JSP] form 태그 (0) | 2024.07.02 |
[JSP] 구현 2 - 상세보기 (1) | 2024.07.02 |
[JSP] 내장 객체 (0) | 2024.06.28 |