지금 학원에서 프로젝트로 쇼핑몰 홈페이지를 작성하고 있는데요
몇날며칠을 찾아봐도 해답이 없어 여기에 여쭤봅니다 ㅠㅠ
이클립스 루나, 톰캣 7.0, 파일 업로드는 cos.jar로 이용하고 있구요
기존에 있던 예제에 저희가 필요하다 생각되는 부분을 더 추가하여 발표하려하는데
제가 담당한 부분은 관리자가 쇼핑몰 상품을 등록하는 부분입니다.
상품이미지가 한개 첨부되던걸 메인, 내부, 상세이미지 세개로 늘리고
상품 부제목을 추가한게 다입니다
부제목 name2까지는 구현이 되었고
상품이미지 업로드 과정에서 앞서 말씀드린 새로 추가한 내부이미지 image2와 상세이미지 image3가 업로드 되지 않고 있는 상황입니다 ㅠㅠ
선생님한테 여쭈어도 뾰족한 수가 없어서....
우선 제가 손댄 부분들과 중요한 파일들의 목록을 보여드릴게요
소스 올려드리겠습니다
ProductVO.java는 getter setter 추가한거구요
-----------------------------------------------------------------
package com.nonage.dto;
import java.sql.Timestamp;
public class ProductVO
{
private int pseq;
private String name;
private String name2; //부제목
private String kind;
private int price1;
private int price2;
private int price3;
private String content;
private String image;
private String image2; //내부메인이미지
private String image3; //내부상세이미지
private String useyn;
private String bestyn;
private Timestamp indate;
}
*******중략*******
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getImage2() {
return image2;
}
public void setImage2(String image2) {
this.image2 = image2;
}
public String getImage3() {
return image3;
}
public void setImage3(String image3) {
this.image3 = image3;
}
}
----------------------------------------------------
productWrite.jsp 입니다.
관리자가 상품을 등록하는 페이지입니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/admin/header.jsp"%>
<%@ include file="/admin/sub_menu.jsp"%>
<article>
<h1>상품등록</h1>
<form name="frm" method="post" enctype="multipart/form-data">
<table id="list">
<tr>
<th>상품분류</th>
<td colspan="5"><select name="kind">
<c:forEach items="${kindList}" var="kind" varStatus="status">
<option value="${status.count}">${kind}</option>
</c:forEach>
</select>
<tr>
<th>상품명</th>
<td width="343" colspan="5"><input type="text" name="name"
size="47" maxlength="100"></td>
</tr>
<tr>
<th>부제목</th>
<td width="343" colspan="5"><input type="text" name="name2"
size="47" maxlength="100"></td>
</tr>
<tr>
<th>딜가격[A]</th>
<td width="70"><input type="text" name="price1" size="11"
onKeyUp='NumFormat(this)'></td>
<th>원가[B]</th>
<td width="70"><input type="text" name="price2" size="11"
onBlur="go_ab()" onKeyUp='NumFormat(this)'></td>
<th>[B-A]</th>
<td width="72"><input type="text" name="price3" size="11"
readonly onKeyUp='NumFormat(this)'></td>
</tr>
<tr>
<th>상세설명</th>
<td colspan="5"><textarea name="content" rows="8" cols="70"></textarea>
</td>
</tr>
<tr>
<th>상품이미지(메인)</th>
<td width="343" colspan="5"><input type="file" name="image">
</td>
</tr>
<tr>
<th>상품이미지(내부)</th>
<td width="343" colspan="5"><input type="file" name="image2">
</td>
</tr>
<tr>
<th>상세이미지</th>
<td width="343" colspan="5"><input type="file" name="image3">
</td>
</tr>
</table>
<input class="btn" type="button" value="등록" onClick="go_save()">
<input class="btn" type="button" value="취소" onClick="go_mov()">
</form>
</article>
<%@ include file="/admin/footer.jsp"%>
</body>
</html>
------------------------------------------------------------------------
ProductDAO.java 입니다
package com.nonage.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import util.DBManager;
import com.nonage.dto.ProductVO;
public class ProductDAO {
// 외부에서 인스턴스 생성을 막음
private ProductDAO() {
}
private static ProductDAO instance = new ProductDAO();
public static ProductDAO getInstance() {
return instance;
}
// ArrayList 객체를 저장하는 임시 공간
public ArrayList<ProductVO> listNewProduct() {
ArrayList<ProductVO> productList = new ArrayList<ProductVO>();
String sql = "select * from new_pro_view";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
ProductVO product = new ProductVO();
product.setPseq(rs.getInt("pseq"));
product.setName(rs.getString("name"));
product.setPrice1(rs.getInt("price1"));
product.setPrice2(rs.getInt("price2"));
product.setImage(rs.getString("image"));
//product.setName2(rs.getString("name2"));
productList.add(product);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(conn, pstmt, rs);
}
return productList;
}
public ArrayList<ProductVO> listBestProduct() {
ArrayList<ProductVO> productList = new ArrayList<ProductVO>();
String sql = "select * from best_pro_view";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
ProductVO product = new ProductVO();
product.setPseq(rs.getInt("pseq"));
product.setName(rs.getString("name"));
product.setPrice1(rs.getInt("price1"));
product.setPrice2(rs.getInt("price2"));
product.setImage(rs.getString("image"));
//product.setName2(rs.getString("name2"));
productList.add(product);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(conn, pstmt, rs);
}
return productList;
}
public ProductVO getProduct(String pseq) {
ProductVO product = null;
String sql = "select * from product where pseq=?";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DBManager.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, pseq);
rs = pstmt.executeQuery();
if (rs.next()) {
product = new ProductVO();
product.setPseq(rs.getInt("pseq"));
product.setName(rs.getString("name"));
product.setKind(rs.getString("kind"));
product.setPrice1(rs.getInt("price1"));
product.setPrice2(rs.getInt("price2"));
product.setPrice3(rs.getInt("price3"));
product.setContent(rs.getString("content"));
product.setImage(rs.getString("image"));
product.setUseyn(rs.getString("useyn"));
product.setBestyn(rs.getString("bestyn"));
product.setIndate(rs.getTimestamp("indate"));
product.setName2(rs.getString("name2"));
product.setImage2(rs.getString("image2"));
product.setImage3(rs.getString("image3"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(con, pstmt, rs);
}
return product;
}
public ArrayList<ProductVO> listKindProduct(String kind) {
ArrayList<ProductVO> productList = new ArrayList<ProductVO>();
String sql = "select * from product where kind=?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, kind);
rs = pstmt.executeQuery();
while (rs.next()) {
ProductVO product = new ProductVO();
product.setPseq(rs.getInt("pseq"));
product.setName(rs.getString("name"));
product.setPrice2(rs.getInt("price2"));
product.setImage(rs.getString("image"));
//product.setName2(rs.getString("name2"));
productList.add(product);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(conn, pstmt, rs);
}
return productList;
}
/* 관리자 모드에서 사용되는 메소드 */
public int totalRecord(String product_name) {
int total_pages = 0;
String sql = "select count(*) from product where name like '%'||?||'%'";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet pageset = null;
try {
con = DBManager.getConnection();
pstmt = con.prepareStatement(sql);
if (product_name.equals("")) {
pstmt.setString(1, "%");
} else {
pstmt.setString(1, product_name);
}
pageset = pstmt.executeQuery();
if (pageset.next()) {
total_pages = pageset.getInt(1); // 레코드의 개수
pageset.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(con, pstmt);
}
return total_pages;
}
static int view_rows = 5; // 페이지의 개수
static int counts = 5; // 한 페이지에 나타낼 상품의 개수
// 페이지 이동을 위한 메소드
public String pageNumber(int tpage, String name) {
String str = "";
int total_pages = totalRecord(name);
int page_count = total_pages / counts + 1;
if (total_pages % counts == 0) {
page_count--;
}
if (tpage < 1) {
tpage = 1;
}
int start_page = tpage - (tpage % view_rows) + 1;
int end_page = start_page + (counts - 1);
if (end_page > page_count) {
end_page = page_count;
}
if (start_page > view_rows) {
str += "<a href='NonageServlet?command=admin_product_list&tpage=1&key="
+ name + "'><<</a> ";
str += "<a href='NonageServlet?command=admin_product_list&tpage="
+ (start_page - 1);
str += "&key=<%=product_name%>'><</a> ";
}
for (int i = start_page; i <= end_page; i++) {
if (i == tpage) {
str += "<font color=red>[" + i + "] </font>";
} else {
str += "<a href='NonageServlet?command=admin_product_list&tpage="
+ i + "&key=" + name + "'>[" + i + "]</a> ";
}
}
if (page_count > end_page) {
str += "<a href='NonageServlet?command=admin_product_list&tpage="
+ (end_page + 1) + "&key=" + name
+ "'> > </a> ";
str += "<a href='NonageServlet?command=admin_product_list&tpage="
+ page_count + "&key=" + name
+ "'> > > </a> ";
}
return str;
}
public ArrayList<ProductVO> listProduct(int tpage, String product_name) {
ArrayList<ProductVO> productList = new ArrayList<ProductVO>();
String str = "select pseq, indate, name, price1, price2, useyn, bestyn, name2 "
+ " from product where name like '%'||?||'%' order by pseq desc";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int absolutepage = 1;
try {
con = DBManager.getConnection();
absolutepage = (tpage - 1) * counts + 1;
pstmt = con.prepareStatement(str, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
if (product_name.equals("")) {
pstmt.setString(1, "%");
} else {
pstmt.setString(1, product_name);
}
rs = pstmt.executeQuery();
if (rs.next()) {
rs.absolute(absolutepage);
int count = 0;
while (count < counts) {
ProductVO product = new ProductVO();
product.setPseq(rs.getInt(1));
product.setIndate(rs.getTimestamp(2));
product.setName(rs.getString(3));
product.setPrice1(rs.getInt(4));
product.setPrice2(rs.getInt(5));
product.setUseyn(rs.getString(6));
product.setBestyn(rs.getString(7));
product.setName2(rs.getString(8));
productList.add(product);
if (rs.isLast()) {
break;
}
rs.next();
count++;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(con, pstmt, rs);
}
return productList;
}
public int insertProduct(ProductVO product) {
int result = 0;
String sql = "insert into product ("
+ "pseq, kind, name, price1, price2, price3, content, image, name2, image2, image3) "
+ "values(product_seq.nextval, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DBManager.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, product.getKind());
pstmt.setString(2, product.getName());
pstmt.setInt(3, product.getPrice1());
pstmt.setInt(4, product.getPrice2());
pstmt.setInt(5, product.getPrice3());
pstmt.setString(6, product.getContent());
pstmt.setString(7, product.getImage());
pstmt.setString(8, product.getName2());
pstmt.setString(9, product.getImage2());
pstmt.setString(10, product.getImage3());
result = pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("추가 실패");
e.printStackTrace();
} finally {
DBManager.close(con, pstmt);
}
return result;
}
public int updateProduct(ProductVO product) {
int result = -1;
String sql = "update product set kind=?, useyn=?, name=?"
+ ", price1=?, price2=?, price3=?, content=?, image=?, bestyn=?"
+ ", name2=?, image2=?, image3=? "
+ "where pseq=?";
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DBManager.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, product.getKind());
pstmt.setString(2, product.getUseyn());
pstmt.setString(3, product.getName());
pstmt.setInt(4, product.getPrice1());
pstmt.setInt(5, product.getPrice2());
pstmt.setInt(6, product.getPrice3());
pstmt.setString(7, product.getContent());
pstmt.setString(8, product.getImage());
pstmt.setString(9, product.getBestyn());
pstmt.setInt(10, product.getPseq());
pstmt.setString(11, product.getName2());
pstmt.setString(12, product.getImage2());
pstmt.setString(13, product.getImage3());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(con, pstmt);
}
return result;
}
}
--------------------------------------------------
그리고 지금 제가 제일 갑갑한 AdminProductWriteAction.java 입니다
위에 jsp로 작성된 파일들을 얘를 통해서 업로드하게 되어있는데요
인터넷에 아무리 찾아보아도 다들 서블릿으로 작성하셔서 ㅠㅠ
여기서 Enumeration 을 어떻게 적용해야할지 모르겠어요 ㅠㅠㅠㅠㅠㅠㅠㅠㅠ
package com.nonage.admin.controller.action;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.nonage.controller.action.Action;
import com.nonage.dao.ProductDAO;
import com.nonage.dto.ProductVO;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
public class AdminProductWriteAction implements Action {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String url = "NonageServlet?command=admin_product_list";
HttpSession session = request.getSession();
int sizeLimit = 5 * 1024 * 1024;
String savePath = "product_images";
ServletContext context = session.getServletContext();
String uploadFilePath = context.getRealPath(savePath);
MultipartRequest multi = new MultipartRequest(request, // 1. 요청 객체
uploadFilePath, // 2. 업로드될 파일이 저장될 파일 경로명
sizeLimit, // 3. 업로드될 파일의 최대 크기(5Mb)
"UTF-8", // 4. 인코딩 타입 지정
new DefaultFileRenamePolicy() // 5. 덮어쓰기를 방지 위한 부분
); // 이 시점을 기해 파일은 이미 저장이 되었다
Enumeration formNames = multi.getFileNames();
ProductVO productVO = new ProductVO();
productVO.setKind(multi.getParameter("kind"));
productVO.setName(multi.getParameter("name"));
productVO.setPrice1(Integer.parseInt(multi.getParameter("price1")));
productVO.setPrice2(Integer.parseInt(multi.getParameter("price2")));
productVO.setPrice3(Integer.parseInt(multi.getParameter("price2"))
- Integer.parseInt(multi.getParameter("price1")));
productVO.setContent(multi.getParameter("content"));
productVO.setImage(multi.getFilesystemName("image"));
productVO.setImage2(multi.getFilesystemName("image2"));
productVO.setImage3(multi.getFilesystemName("image3"));
productVO.setName2(multi.getParameter("name2"));
ProductDAO productDAO = ProductDAO.getInstance();
productDAO.insertProduct(productVO);
response.sendRedirect(url);
}
}
--------------------------------------------------------------------
소스는 일단 너무 기니 여기까지 구요 더 필요하신 부분 있으면 말씀해주세요 ㅠㅠ
제발 부탁드립니다 ㅠㅠㅠㅠ