-
[ERMaster] shoppingmall - mainWEB/ERMaster 2022. 9. 10. 12:03
Run 시작페이지
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>index.jsp</title> </head> <body> <% response.sendRedirect("shop.do?command=index"); %> </body> </html>
Servlet
package com.sam.shop.controller; import java.io.IOException; 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 com.sam.shop.controller.action.Action; @WebServlet("/shop.do") public class ShoppingMallServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ShoppingMallServlet() { // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String command=request.getParameter("command"); ActionFactory af=ActionFactory.getInstance(); Action ac=af.getAction(command); if(ac!=null) ac.execute(request,response); else System.out.println("ac=null"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ActionFactory
package com.sam.shop.controller; import com.sam.shop.controller.action.Action; public class ActionFactory { private ActionFactory() {} private static ActionFactory itc = new ActionFactory(); public static ActionFactory getInstance() { return itc; } public Action getAction(String command) { Action ac = null; // 이 안에 command가 실행될 코드 들어감 // *배경색 줄무늬로 표시* return ac; } }
Action interface
package com.sam.shop.controller.action; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface Action { public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; }
Server에 Resource 코드 추가
server.xml 파일 코드 수정 해당 프로젝트 이름: WEB12_ShoppingMall <Context docBase="WEB12_ShoppingMall" path="/WEB12_ShoppingMall" reloadable="true" source="org.eclipse.jst.jee.server:WEB12_ShoppingMall"> <Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxIdle="10" maxTotal="20" maxWaitMillis="-1" name="jdbc/myoracle" password="tiger" type="javax.sql.DataSource" url="jdbc:oracle:thin:@127.0.0.1:1521:xe" username="scott"/> </Context>
*jsp 파일 코드는 배경 검은색으로 표시
쇼핑몰 메인 페이지 제작
- 페이지 상단 제작
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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> <meta charset="UTF-8"> <title>hearder.jsp</title> <link href="css/shopping.css" rel="stylesheet"> </head> <body> <div id="wrap"> <header> <!-- 로고, 주 상단 메뉴, 카테고리 메뉴가 표시되는 영역 --> <div id="logo"> <a href=""> <img src="images/logo.png" width="180" height="100"> </a> </div> <nav id="top_menu"><!-- LOGIN CART MYPAGE 등등 --> <ul> <c:choose> <c:when test="${empty loginUser}"> <li><a href="">LOGIN</a></li> <li><a href="">JOIN</a></li> </c:when> <c:otherwise> <li>${loginUser.name}(${loginUser.id})</li> <li><a href="">정보수정</a></li> <li><a href="">LOGOUT</a></li> </c:otherwise> </c:choose> <li><a href="">CART</a></li> <li><a href="">MY PAGE</a></li> <li><a href="">Q&A</a></li> </ul> </nav> <!-- 카테고리 메뉴 Heels Boots Sandals ...등 --> <nav id="catagory_menu"> <ul> <li><a href="">Heels</a></li> <li><a href="">Boots</a></li> <li><a href="">Sandals</a></li> <li><a href="">Sneakers</a></li> <li><a href="">Sleeper</a></li> <li><a href="">On Sale</a></li> </ul> </nav> </header> </body> </html>
2. 쇼핑몰 하단 제작
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%-- footer.jsp --%> <div class="clear"></div> <footer> <hr> <div id="copy"> All contents Copyright 2022 HJKang.co Inc. all rights reserved<br>Content mail : abc@abc.com Tel : +82 02 1234 1234 Fax : +82 02 1233 1233</div> </footer> </div> </body> </html>
3. 쇼핑몰 메인 제작
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%-- main.jsp --%> <%@ include file="header.jsp" %> <!-- 메인 이미지 시작: 각 기능별 페이지를 대표하는 이미지 --> <div id="main_img"> <img src="images/main_img.jpg" style="border-radius:20px 20px 20px 20px; border:2px solid white;"> </div> <!-- 신상품 --> <h2>New Item</h2> <div id="bestProduct"> <c:forEach items="${newList}" var="productVO"> <div id="item"><!-- 상품 한개, 한칸 --> <a href=""> <img src="product_images/${productVO.image }"/> <h3>${productVO.name } - <fmt:formatNumber value="${productVO.price2}" type="currency"/></h3> </a> </div> </c:forEach> </div> <!-- 베스트 상품 --> <h2>Best Item</h2> <div id="bestProduct"> <c:forEach items="${bestList}" var="productVO"> <div id="item"><!-- 상품 한개, 한칸 --> <a href=""> <img src="product_images/${productVO.image }"/> <h3>${productVO.name } - <fmt:formatNumber value="${productVO.price2}" type="currency"/></h3> </a> </div> </c:forEach> </div> <%@ include file="footer.jsp" %>
ActionFatory에 index command 추가
if(command.equals("index")) ac=new IndexAction();
package com.sam.shop.controller.action; import java.io.IOException; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IndexAction implements Action { @Override public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 베스트 상품과 신상품 목록을 조회&리퀘스에 담아서 main.jsp로 이동 ProductDao pdao = ProductDao.getInstance(); ArrayList<ProductVO> bestList = pdao.getBestList(); ArrayList<ProductVO> newList = pdao.getNewList(); request.setAttribute("bestList", bestList); request.setAttribute("newList", newList); RequestDispatcher dispatcher = request.getRequestDispatcher("main.jsp"); dispatcher.forward(request, response); } }
Product Dto
package com.sam.shop.dto; import java.sql.Timestamp; public class ProductVO { private Integer pseq; private String name; private String kind; private Integer price1; private Integer price2; private Integer price3; private String content; private String image; private String useyn; private String bestyn; private Timestamp indate; public Integer getPseq() { return pseq; } public void setPseq(Integer pseq) { this.pseq = pseq; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public Integer getPrice1() { return price1; } public void setPrice1(Integer price1) { this.price1 = price1; } public Integer getPrice2() { return price2; } public void setPrice2(Integer price2) { this.price2 = price2; } public Integer getPrice3() { return price3; } public void setPrice3(Integer price3) { this.price3 = price3; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getUseyn() { return useyn; } public void setUseyn(String useyn) { this.useyn = useyn; } public String getBestyn() { return bestyn; } public void setBestyn(String bestyn) { this.bestyn = bestyn; } public Timestamp getIndate() { return indate; } public void setIndate(Timestamp indate) { this.indate = indate; } }
Product Dao
package com.sam.shop.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class ProductDao { private ProductDao() {} private static ProductDao itc = new ProductDao(); public static ProductDao getInstance() { return itc; } Connection con=null; PreparedStatement pstmt=null; ResultSet rs=null; }
ProductDao에 getBestList, getNewList 추가
public ArrayList<ProductVO> getBestList() { ArrayList<ProductVO> list = new ArrayList<ProductVO>(); String sql = "select * from best_pro_view"; con = Dbman.getConnection(); try { pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while( rs.next() ) { ProductVO pvo = new ProductVO(); pvo.setPseq( rs.getInt("pseq") ); pvo.setName( rs.getString("name") ); pvo.setPrice2( rs.getInt("price2") ); pvo.setImage( rs.getString("image") ); list.add(pvo); } } catch (SQLException e) { e.printStackTrace(); } finally { Dbman.close(con, pstmt, rs); } return list; } public ArrayList<ProductVO> getNewList() { ArrayList<ProductVO> list = new ArrayList<ProductVO>(); String sql = "select * from new_pro_view"; con = Dbman.getConnection(); try { pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while( rs.next() ) { ProductVO pvo = new ProductVO(); pvo.setPseq( rs.getInt("pseq") ); pvo.setName( rs.getString("name") ); pvo.setPrice2( rs.getInt("price2") ); pvo.setImage( rs.getString("image") ); list.add(pvo); } } catch (SQLException e) { e.printStackTrace(); } finally { Dbman.close(con, pstmt, rs); } return list; }
css
body{background:#FDE8FF; margin:0; padding:0; font-size:0.75em;} #wrap{width:971px; text-align:left; margin:0 auto; background:#FDE8FF; min-height:780px;} #logo{float:left; width:180px; height:100px; margin:40px 0 0 40px;} nav#top_menu{float:right; margin:20px 64px 0 0;} nav#top_menu ul li{display:block; height:30px; float:left; font-family:Verdana; font-size:130%;} nav#top_menu a{text-decoration:none; display:inline-block; width:100px; text-align:center; color:#6F0C78; font-weight:bold;} nav#top_menu a:hover{color:blue; font-weight:bold;} nav#catagory_menu{margin:20px 0; float:right;} nav#catagory_menu ul{list-style:none; font-family:Verdana;} nav#catagory_menu ul li{float:left; margin:0 15px; font-size:14pt;} nav#catagory_menu a{display:block; height:20px; padding:10px; text-decoration:none; color:#6F0C78;} nav#catagory_menu a:hover{background-repeat:repeat-x; background-position:bottom; background-image:url(../images/purple.gif);} .clear{clear:both;} footer{margin:10px 0 0 0; height:70px; background-image:url(../images/bottom_logo.png); background-repeat:no-repeat; background-position:60px center;} footer #copy{margin:5px 0 0 300px; width:600px;} #bestProduct{border:solid 3px #FFFFFF; border-radius:0px; width:910px; min-height:260px; background:#F4CBFA; padding:10px; text-align:center; margin:0 auto;} #item{width:200px; float:left; margin:0 13px; text-align:center;} #item img{width:190px; height:200px; overflow:hidden; border-radius:20px 20px 20px 20px; border:3px solid gray;} #main_img{text-align:center;} #main_img img{margin:9px 0 0 0;}
생성된 메인 화면
'WEB > ERMaster' 카테고리의 다른 글
[ERMaster] shoppingmall - category (0) 2022.09.10 [ERMaster] shoppingmall - find ID, PW (0) 2022.09.10 [ERMaster] shoppingmall - update (0) 2022.09.10 [ERMaster] shoppingmall - login, logout, join (0) 2022.09.10 [ERMaster] 설치 후 테이블 생성 (0) 2022.09.10