반응형
쇼핑몰 시스템
상품이 없을 때 뜨는 오류 화면을 대신하기 위해서 아래 작업 진행
exceptionNoProductId.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title></title>
</head>
<body>
<jsp:include page="menu.jsp" />
<div class="jumbotron">
<!-- container : 이 안에 내용있다 -->
<div class="container">
<h1 class="display-3">해당 상품이 존재하지 않습니다.</h1>
</div>
</div>
<!-- ----------------오류 처리 내용 시작 ----------------------- -->
<div class="container">
<!-- getRequestURL() : http://localhost/product.jsp -->
<!-- getQueryString() : id=P1234 -->
<p><%=request.getRequestURL()%>?<%=request.getQueryString()%></p>
<p><a href="products.jsp" class="btn btn-secondary">상품 목록»</a>
</div>
<!-- ----------------오류 처리 내용 끝 ----------------------- -->
<jsp:include page="footer.jsp" />
</body>
</html>
페이지가 없을 때 뜨는 오류 화면을 대신하기 위해서 아래 작업 진행
exceptionNoPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title></title>
</head>
<body>
<jsp:include page="menu.jsp" />
<div class="jumbotron">
<!-- container : 이 안에 내용있다 -->
<div class="container">
<h1 class="display-3">요청하신 페이지를 찾을 수 없습니다.</h1>
</div>
</div>
<!-- ----------------오류 처리 내용 시작 ----------------------- -->
<div class="container">
<!-- getRequestURL() : http://localhost/product.jsp -->
<!-- getQueryString() : id=P1234 -->
<p><%=request.getRequestURL()%>%></p>
<p><a href="products.jsp" class="btn btn-secondary">상품 목록»</a>
</div>
<!-- ----------------오류 처리 내용 끝 ----------------------- -->
<jsp:include page="footer.jsp" />
</body>
</html>
product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="kr.or.ddit.vo.ProductVO"%>
<%@ page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page errorPage="/exceptionNoPage.jsp" %>
<%
/*
요청URI : product.jsp
요청파라미터 : productId=P1234
요청방식 : get(목록, 검색, 상세)
*/
// 싱글톤
ProductRepository productDAO =
ProductRepository.getInstance();
String productId = request.getParameter("productId"); // P1234
out.print("<p>"+productId+"</p>"); // P1234
ProductVO productVO = productDAO.getProductById(productId);
out.print("<p>"+productVO+"</p>");
%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title></title>
</head>
<body>
<!-- include 액션 태그 -->
<jsp:include page="menu.jsp" /> <!-- 파라미터가 없기 때문에 이렇게 사용 가능 -->
<!-- --------------------상품상세 시작-------------------- -->
<div class="jumbotron">
<!-- container : 이 안에 내용있다 -->
<div class="container">
<h1 class="display-3">상품 정보</h1>
</div>
</div>
<!-- 내용 -->
<div class="container">
<div class="row">
<div class="col-md-5">
<img src="/images/<%=productVO.getFilename()%>"
style="width:100%;">
</div>
<div class="col-md-6">
<h3><%=productVO.getPname()%></h3>
<p><%=productVO.getDescription()%></p>
<p>
<b>상품 코드 : </b>
<span class="badge bage-danger">
<%=productVO.getProductId()%>
</span>
</p>
<p><b>제조사 : </b><%=productVO.getManufacturer()%></p>
<p><b>분류 : </b><%=productVO.getCategory()%></p>
<p><b>재고 수 : </b><%=productVO.getUnitsInStock()%></p>
<h4><%=productVO.getUnitPrice()%></h4>
<p>
<!-- 폼태그, 폼페이지
action : 요청URI
요청URI : addCart.jsp
요청파라미터 : id=P1234
요청방식 : post
-->
<form name="addForm" action="addCart.jsp?productId=<%=productVO.getProductId()%>"
method="post">
<!-- 폼데이터 -->
<!-- 상품 주문 : 장바구니에 상품을 넣음
addToCart() : 핸들러 함수
-->
<a href="#" class="btn btn-info" onclick="addToCart()">삼품주문»</a>
<!-- 장바구니 : 장바구니에 담겨진 상품 목록 확인 -->
<a href="cart.jsp" class="btn btn-warning">장바구니»</a>
<a href="products.jsp" class="btn btn-secondary">상품 목록»</a>
</form>
</div>
</div>
</div>
<jsp:include page="footer.jsp" />
</body>
</html>
- errorPage 설정 (상품이 존재하지 않을때) : 500
product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="kr.or.ddit.vo.ProductVO"%>
<%@ page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page errorPage="/exceptionNoPage.jsp" %>
<%
/*
요청URI : product.jsp
요청파라미터 : productId=P1234
요청방식 : get(목록, 검색, 상세)
*/
// 싱글톤
ProductRepository productDAO =
ProductRepository.getInstance();
String productId = request.getParameter("productId"); // P1234
out.print("<p>"+productId+"</p>"); // P1234
ProductVO productVO = productDAO.getProductById(productId);
out.print("<p>"+productVO+"</p>");
productVO.toString(); //?productId=P000 => null.toString() => 문제 발생
%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title></title>
</head>
<body>
<!-- include 액션 태그 -->
<jsp:include page="menu.jsp" /> <!-- 파라미터가 없기 때문에 이렇게 사용 가능 -->
<!-- --------------------상품상세 시작-------------------- -->
<div class="jumbotron">
<!-- container : 이 안에 내용있다 -->
<div class="container">
<h1 class="display-3">상품 정보</h1>
</div>
</div>
<!-- 내용 -->
<div class="container">
<div class="row">
<div class="col-md-5">
<img src="/images/<%=productVO.getFilename()%>"
style="width:100%;">
</div>
<div class="col-md-6">
<h3><%=productVO.getPname()%></h3>
<p><%=productVO.getDescription()%></p>
<p>
<b>상품 코드 : </b>
<span class="badge bage-danger">
<%=productVO.getProductId()%>
</span>
</p>
<p><b>제조사 : </b><%=productVO.getManufacturer()%></p>
<p><b>분류 : </b><%=productVO.getCategory()%></p>
<p><b>재고 수 : </b><%=productVO.getUnitsInStock()%></p>
<h4><%=productVO.getUnitPrice()%></h4>
<p>
<!-- 폼태그, 폼페이지
action : 요청URI
요청URI : addCart.jsp
요청파라미터 : id=P1234
요청방식 : post
-->
<form name="addForm" action="addCart.jsp?productId=<%=productVO.getProductId()%>"
method="post">
<!-- 폼데이터 -->
<!-- 상품 주문 : 장바구니에 상품을 넣음
addToCart() : 핸들러 함수
-->
<a href="#" class="btn btn-info" onclick="addToCart()">삼품주문»</a>
<!-- 장바구니 : 장바구니에 담겨진 상품 목록 확인 -->
<a href="cart.jsp" class="btn btn-warning">장바구니»</a>
<a href="products.jsp" class="btn btn-secondary">상품 목록»</a>
</form>
</div>
</div>
</div>
<jsp:include page="footer.jsp" />
</body>
</html>
- errorPage 설정 (페이지가 존재하지 않을때) : 404
web.xml (변경 또는 추가)
<error-page>
<error-code>404</error-code>
<location>/exceptionNoPage.jsp</location>
</error-page>
반응형
'스프링' 카테고리의 다른 글
[스프링] 20장 과제 (0) | 2024.04.17 |
---|---|
[스프링] 19장 필터, 쇼핑몰 시스템7 (0) | 2024.04.17 |
[스프링] 17장 예외 처리 (0) | 2024.04.16 |
[스프링] 16.5장 과제 (0) | 2024.04.15 |
[스프링] 16장 시큐리티, 쇼핑몰 시스템5(시큐리티 작업) (0) | 2024.04.15 |