💡 핵심 요약 (TL;DR)
Servlet상품3: 상품목록·상품리스트의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* .ag -> Action_DAO_DB ->.jsp로 이동하니까 action객체사용을 적용할 수 있다.
AdminGoodsFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가
.ag -> Action_DAO_DB ->.jsp로 이동하니까 action객체사용
1 2 3 4 5 6 7 8
| }else if(command.equals("/AdminGoodsList.ag")){ System.out.println("C: /AdminGoodsListAction.ag 호출"); action = new AdminGoodsListAction(); try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } }
|
AdminGoodsListAction.java 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class AdminGoodsListAction implements Action {
@Override public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("M : AdminGoodsListAction의 execute() 호출"); HttpSession session = request.getSession(); String id = (String)session.getAttribute("id"); ActionForward forward = new ActionForward(); if(id == null || !id.equals("admin")){ forward.setPath("./Main.me"); forward.setRedirect(true); return forward; } AdminGoodsDAO agdao = new AdminGoodsDAO(); List<GoodsDTO> goodsList = agdao.getGoodsList();; System.out.println("M : "+goodsList); request.setAttribute("goodsList", goodsList); forward.setPath("./admingoods/admin_goods_list.jsp"); forward.setRedirect(false); return forward; } }
|
admin_goods_list.jsp 생성
관리자만 페이지 볼 수 있게 하려면
- session을 활용하여 코드를 작성하면 된다
1 2 3 4
| String id = (String) session.getAttribute("id"); if(!id.equals("admin")){ response.sendRedirect("./MemberLogin.me"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <% // request 영역에 저장 //request.setAttribute("goodsList", goodsList); // request영역에서 정보를 꺼내서 테이블에 추가 List<GoodsDTO> goodsList = (List<GoodsDTO>)request.getAttribute("goodsList"); //object->list타입 캐스팅 //관리자만 페이지 볼 수 있게 추가 String id = (String) session.getAttribute("id"); if(id == null || !id.equals("admin")){ response.sendRedirect("./Main.me"); } %> <fieldset> <legend>관리자용 상품등록</legend> <table border="1"> <tr> <td>번호</td> <td>카테고리</td> <td>사진</td> <td>상품명</td> <td>가격</td> <td>수량</td> <td>등록일</td> <td>수정·삭제</td> </tr> <% //List는 가변배열이므로 length가 없고 size가 존재한다. //size()메서드는 배열의 요소의 갯수를 리턴 for(int i=0; i<goodsList.size(); i++){ //List 한칸의 정보 -> GoodsDTO 객체 하나로 이동 GoodsDTO gdto = (GoodsDTO) goodsList.get(i); %> <tr> <td><%=gdto.getGno() %></td> <td><%=gdto.getCategory() %></td> <td> <img src="./upload/<%=gdto.getImage().split(",")[0]%>" height="100"> </td> <td><%=gdto.getName() %></td> <td><%=gdto.getPrice() %>원</td> <td><%=gdto.getAmount() %>개</td> <td><%=gdto.getDate() %></td> <td> <input type="button" class="btn" value="수정" onclick="location.href='./AdminGoodsModify.ag?gno=<%=gdto.getGno()%>'"> <input type="button" class="btn" value="삭제" onclick="location.href='./AdminGoodsDeleteAction.ag?gno=<%=gdto.getGno()%>'"> </td> </tr> <% } %> </table> </fieldset>
|
AdminGoodsDAO.java 생성 후 getGoodsList()메서드 코드 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public List<GoodsDTO> getGoodsList() { List<GoodsDTO> goodsList = new ArrayList<GoodsDTO>(); try { getCon(); sql = "select * from itwill_goods"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ GoodsDTO gdto = new GoodsDTO(); gdto.setGno(rs.getInt("gno")); gdto.setCategory(rs.getString("category")); gdto.setName(rs.getString("name")); gdto.setPrice(rs.getInt("price")); gdto.setColor(rs.getString("color")); gdto.setAmount(rs.getInt("amount")); gdto.setSize(rs.getString("size")); gdto.setContent(rs.getString("content")); gdto.setImage(rs.getString("image")); gdto.setBest(rs.getInt("best")); gdto.setDate(rs.getDate("date"));
goodsList.add(gdto); } System.out.println("DAO : 상품목록 모두 저장완료! "+goodsList); } catch (Exception e) { e.printStackTrace(); } finally { closeDB(); } return goodsList; }
|
#TILITWILL(JAVA)