2023. 4. 5. 16:24ใSpring
ํ๋ฉด ์๋จ์ MY PAGE ๋๋ ์ ๋ ํ์์ด ๋ณด๋ฉด ํ๋ฉด์ฐฝ ๊ตฌ์ฑ
personal_layout.html ์์ฑ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- bootstrap css ๋ก๋ฉ -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<!-- admin_layout๊ณผ ์ด๋ฆฌ๋ ๋ชจ๋ ํ์ด์ง์ ์ด๋ฆฌ๋ ๊ณตํต ์ ์ฉ๋๋ css ๋ก๋ฉ -->
<link rel="stylesheet" href="/css/common.css">
<!-- header์ ์ ์ฉ๋๋ css -->
<link rel="stylesheet" href="/css/fragment/header.css">
<!-- content ํ์ด์ง๊ฐ ์ด๋ฆด ๋ ๊ฐ์ด ์ด๋ฆฌ๋ css (ํ์ด์ง๋ง๋ค ๋ฐ๋๋ css) -->
<th:block layout:fragment="content_css"></th:block>
</head>
<body>
<div class="container">
<!-- ํ -->
<div class="row" style="margin-bottom: 30px;">
<!-- ์ด -->
<div class="col">
<!-- headerFragment๋ผ๋ ์ด๋ฆ์ผ๋ก ์ง์ ๋ layout ํ์ด์ง๋ก ๋์ฒด๋จ -->
<th:block th:replace="~{fragment/header::headerFragment}"></th:block>
</div>
</div>
<div class="row">
<div class="col-3"> <!-- ํ์ 12 ๋ถ๋ถ์ ์ฌ์ด์ฆ ์ง์ ๊ฐ๋ฅ -->
<!-- adminSideFragment๋ผ๋ ์ด๋ฆ์ผ๋ก ์ง์ ๋ layout ํ์ด์ง๋ก ๋์ฒด๋จ -->
<th:block th:replace="~{fragment/personal_side::personalSideFragment}"></th:block>
</div>
<div class="col-9">
<!-- content๋ผ๋ ์ด๋ฆ์ผ๋ก ์ง์ ๋ layout ํ์ด์ง ์ด๋ฆผ (๋ฐ๋๋ ํ๋ฉด) -->
<th:block layout:fragment="content"></th:block>
</div>
</div>
</div>
<!-- Jquery ๋ฌธ๋ฒ ๋ก๋ฉ -->
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<!-- bootstrap js ๋ก๋ฉ -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<!-- header.js ๋ก๋ฉ -->
<script type="text/javascript" src="/js/fragment/header.js"></script>
<!-- ์ฐํธ๋ฒํธ api -->
<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
<!-- content ํ์ด์ง๊ฐ ์ด๋ฆด ๋ ๊ฐ์ด ์ด๋ฆฌ๋ js -->
<th:block layout:fragment="content_js"></th:block>
</body>
</html>
personal_side.html ์์ฑ ๋ html ์ฐ๊ฒฐ fragment๋ก ํ๋ฉด ๊ตฌ์ฑ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="personalSideFragment">
<div class="row">
<div class="col"> <!-- active -->
<div class="list-group">
<!-- ํ์ ์ฌ์ด๋ ๋ฉ๋ด -->
<a href=""
class="list-group-item list-group-item-action">
์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก
</a>
<a href=""
class="list-group-item list-group-item-action">
๊ตฌ๋งค ๋ด์ญ
</a>
<a href=""
class="list-group-item list-group-item-action">
๊ฐ์ธ์ ๋ณด์์
</a>
</div>
</div>
</div>
</th:block>
</html>
MY PAGE ๋๋ฅด๋ฉด ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ํ๋ฉด์ผ๋ก ์ด๋
header.html์ MY PAGE aํ๊ทธ ๊ฐ์ธ์ controller ์ด๋
<a th:href="@{/cart/cartList}">MY PAGE</a>
controller ์์ฑ. ํ์ด์ง ์ด๋ ๊ฒฝ๋ก ์ค์
//์ฅ๋ฐ๊ตฌ๋ ํ์ด์ง
@GetMapping("/cartList")
public String cartList() {
return "content/cart/cart_list";
}
cart_list.html ์์ฑ
personal_layout๊ณผ ๊ฐ์ด ์ด๋ฆฌ๊ธฐ ๋๋ฌธ์ fragment ์ค์
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{fragment/personal_layout}">
<!-- ์ฐ๊ฒฐํด์ผ ํ ์ธ๋ถ css ํ์ผ -->
<th:block layout:fragment="content_css">
<!--<link rel="stylesheet" href="/css/test.css(๋ณ๊ฒฝ)">--> <!-- ์ธ๋ถ css ์ฐ๊ฒฐํ๋ ์ฝ๋๋ฅผ block ์์ ์์ฑ layout์์ ๊ฐ์ด ์ด์ด์ค -->
</th:block>
<th:block layout:fragment="content">
์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก
</th:block>
<!-- ์ฐ๊ฒฐํด์ผ ํ ์ธ๋ถ js ํ์ผ-->
<th:block layout:fragment="content_js">
<!--<script type="text/javascript" src="/js/test.js(๋ณ๊ฒฝ)"></script> --> <!-- ์ธ๋ถ js ์ฐ๊ฒฐํ๋ ์ฝ๋๋ฅผ block ์์ ์์ฑ layout์์ ๊ฐ์ด ์ด์ด์ค -->
</th:block>
</html>
๋ฉ๋ด์ ์นดํ ๊ณ ๋ฆฌ ๋ชฉ๋ก ์ ๋ธ. > ๋งค๋ฒ ์ปจํธ๋กค๋ฌ์์ ๋ชฉ๋ก ์กฐํ ์ฟผ๋ฆฌ ๋ฐ๋ณต ํด์ผํจ.
์นดํ ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํํ๋ ์ธํฐ์ ํธ ์์ฑํ์ฌ ์ฒ๋ฆฌ
CategoryInterceptor ํด๋์ค ์์ฑํ์ฌ HandlerInterceptor ๊ตฌํ
๋ฉ์๋ ์คํ์ ์ํด ๊ทธ ์ ์ itemService ์์กด์ฑ ์ฃผ์ .
postHandle ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ์นดํ ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํ ๋ฉ์๋ ์คํ.
//์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํ
public class CategoryInterceptor implements HandlerInterceptor{
//์์กด์ฑ ์ฃผ์
@Resource(name = "itemService")
private ItemService itemService;
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//์ฌ์ฉ์ค์ธ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํ
modelAndView.addObject("categoryList", itemService.getCateListInUse());
}
}
IntercepterConfig ํด๋์ค์์ ์ธํฐ์ ํฐ ์ธ์ ์ธ ์ง ์ค์ .
์ํ ๋ชฉ๋ก, ์ํ ์์ธ ์กฐํ, ์ฅ๋ฐ๊ตฌ๋ ํ์ด์ง ์ด๋ํ ๋ ์ธํฐ์ ํฐ ํ์.
์นดํ ๊ณ ๋ฆฌ ์ธํฐ์ ํฐ์ ๋ํ ๊ฐ์ฒด ์์ฑํ์ฌ ๋ฐ์ดํฐ ๋ฆฌํด
@Bean
public CategoryInterceptor getCategoryInterceptor() {
return new CategoryInterceptor();
}
์ธํฐ์ ํฐ ์ธ์ ์คํ๋ ์ง ์ค์
//์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํ
registry.addInterceptor(getCategoryInterceptor())
.addPathPatterns("/item/**") //์ํ ๊ด๋ จ ํ์ด์ง ๋ค์ด๊ฐ ๋๋ง๋ค
.addPathPatterns("/cart/**") //์ฅ๋ฐ๊ตฌ๋ ๊ด๋ จ
.excludePathPatterns("/**/*Ajax"); //๋ชจ๋ ์ปจํธ๋กค๋ฌ์ Ajax๋ก ๋๋๋ ์ด๋๊ฒฝ๋ก ์ ์ธ
์ปจํธ๋กค๋ฌ์์ ์ฌ์ฉ์ค์ธ ์ํ ๋ชฉ๋ก ์กฐํํ๋ ๋ฉ์๋ ์ญ์ ํด์ฃผ๊ณ ์ธํฐ์ ํฐ๋ก ๋ค ์ฒ๋ฆฌ๋๊ฒ ํจ.
์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ํ๋ฉด ๊ทธ๋ฆฌ๊ธฐ
์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ ๊ธฐ๋ฅ.
์กฐ์ธ ์ฌ์ฉํ์ฌ ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ ์ฟผ๋ฆฌ ์์ฑ
<!-- ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ -->
<select id="getCartList" resultMap="cart">
SELECT CART_CODE
, ITEM.ITEM_CODE
, ITEM_NAME
, ITEM_PRICE
, CART_CNT
, ATTACHED_FILE_NAME
, CART_CNT * ITME_PRICE AS TOTAL_PRICE
FROM SHOP_ITEM ITEM, SHOP_CART CART, ITEM_IMG IMG
WHERE ITEM.ITEM_CODE = IMG.ITEM_CODE
AND ITEM.ITEM_CODE = CART.ITEM_CODE
WHERE IS_MAIN = 'Y'
AND MEM_ID = #{memId}
</select>
์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ ์ ์ํ ์ ๋ณด์ ์ด๊ฐ๊ฒฉ ๋ฐ์ดํฐ ๋ด์์ฌ ๊ณณ์ด ํ์.
CartVO์ ๋ณ์ ์ถ๊ฐ
private int totalPrice;
private ItemVO itemVO; //CartVO๋ ์ํ ์ ๋ณด ํ๋๋ฅผ ๊ฐ์ง.
CartMapper resultMap ์์ฑ ์ ์ฃผ์!
itemVO๋ association์ฌ์ฉํ์ฌ resultMap ์์ฑ
itemVO๋ imgList ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๊น์ง ๋ค ๋ฐ์์ด.
TOTAL_PRICE๋ ์ด๋ค resultMap์๋ ์๊ธฐ ๋๋ฌธ์ ์ถ๊ฐ ์์ฑ.
<mapper namespace="cartMapper">
<resultMap type="com.study.shop.cart.vo.CartVO" id="cart">
<id column="CART_CODE" property="cartCode"/>
<result column="ITEM_CODE" property="itemCode"/>
<result column="MEM_ID" property="memId"/>
<result column="REG_DATE" property="regDate"/>
<result column="CART_CNT" property="cartCnt"/>
<result column="TOTAL_PRICE" property="totalPrice"/>
<association property="itemVO" resultMap="itemMapper.item"></association>
</resultMap>
CartService ์์ฑ
//์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ
List<CartVO> getCartList(String memId);
CartServiceImpl ์์ฑ
//์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ
@Override
public List<CartVO> getCartList(String memId) {
return sqlSession.selectList("cartMapper.getCartList", memId);
}
CartController ์์ฑ
//์ฅ๋ฐ๊ตฌ๋ ํ์ด์ง
@GetMapping("/cartList")
public String cartList(Model model, HttpSession session) {
//์ธ์
์์ memId ๋นผ๊ธฐ
MemberVO loginInfo = (MemberVO)session.getAttribute("loginInfo");
//์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์กฐํ
List<CartVO> cartList = cartService.getCartList(loginInfo.getMemId());
model.addAttribute("cartList", cartList);
//์ด ๊ตฌ๋งค๊ธ์ก ๊ณ์ฐ
int finalPrice = 0;
for(CartVO cart : cartList) {
finalPrice += cart.getTotalPrice();
}
model.addAttribute("finalPrice", finalPrice);
return "content/cart/cart_list";
}
}
๊ตฌ๋งค๊ธ์ก์ ์ฅ๋ฐ๊ตฌ๋ ์ํ๋ค์ ์ฒดํฌ๋ฐ์ค๋ฅผ ์ฒดํฌํ ๊ฒ๋ค์ ๊ฐ๊ฒฉ ํฉ์ด ๋ค์ด์ด.
controller์์ ์ด ๊ตฌ๋งค๊ธ์ก ๊ณ์ฐํ์ฌ html์ ์ ๋ฌ
cart_list.html์ ๋ฐ์ดํฐ ๋ฟ๋ ค์ฃผ๊ธฐ.
๋ฐ์ดํฐ ๋ฟ๋ฆด ๋ ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ฝ๋ ์ฃผ์!
cartVO ์์ itemVO ์์ itemList์ 0๋ฒ์งธ ๋ฐ์ดํฐ์ attachedFileName ๋ถ๋ฌ์์ผ ํจ.
0๋ฒ์งธ ๋ฐ์ดํฐ์ธ ์ด์ : IS_MIAN = 'Y'์ธ ๊ฒ ํ๋๋ฟ์.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{fragment/personal_layout}">
<!-- ์ฐ๊ฒฐํด์ผ ํ ์ธ๋ถ css ํ์ผ -->
<th:block layout:fragment="content_css">
<!--<link rel="stylesheet" href="/css/test.css(๋ณ๊ฒฝ)">--> <!-- ์ธ๋ถ css ์ฐ๊ฒฐํ๋ ์ฝ๋๋ฅผ block ์์ ์์ฑ layout์์ ๊ฐ์ด ์ด์ด์ค -->
</th:block>
<th:block layout:fragment="content">
<div class="row">
<div class="col">
<div class="row mb-3">
<div class="col">
<table class="table table-hover text-center align-middle">
<colgroup>
<col width="7%">
<col width="15%">
<col width="*">
<col width="15%">
<col width="15%">
<col width="15%">
<col width="7%">
</colgroup>
<thead class="table-head">
<tr>
<td>
<input type="checkbox" class="form-check-input" checked>
</td>
<td>์ํ ์ด๋ฏธ์ง</td>
<td>์ํ๋ช
</td>
<td>๊ฐ ๊ฒฉ</td>
<td>์ ๋</td>
<td>์ด ๊ฐ๊ฒฉ</td>
<td></td>
</tr>
</thead>
<tbody>
<th:block th:if="${#lists.size(cartList) == 0}">
<tr>
<td colspan="7">์ฅ๋ฐ๊ตฌ๋์ ๋ด๊ธด ์ํ์ด ์์ต๋๋ค.</td>
</tr>
</th:block>
<th:block th:if="${#lists.size(cartList) != 0}">
<tr th:each="cart : ${cartList}">
<td>
<input type="checkbox" class="form-check-input" checked>
</td>
<td>
<img width="70px;" th:src="${'/upload/' + cart.itemVO.imgList[0].attachedFileName}">
</td> <!-- ์ผ์ชฝ ์ ๋ ฌ -->
<td class="text-start">[[${cart.itemVO.itemName}]]</td>
<td>[[${#numbers.formatCurrency(cart.itemVO.itemPrice)}]]</td>
<td>
<div class="row">
<div class="col-7">
<input type="number" class="form-control" min="1" th:value="${cart.cartCnt}">
</div>
<div class="col-5 d-grid">
<input type="button" class="btn btn-secondary" value="์์ ">
</div>
</div>
</td>
<td>[[${#numbers.formatCurrency(cart.totalPrice)}]]</td>
<td>
<div class="row">
<div class="col d-grid">
<input type="button" class="btn btn-secondary" value="์ญ์ ">
</div>
</div>
</td>
</tr>
</th:block>
</tbody>
</table>
</div>
</div>
<div class="row mb-3">
<!--offset-10 : 12๋ฑ๋ถ 10๋ฑ๋ถ ๋น์ฐ๊ณ ์์ -->
<div class="offset-9 col-2">๊ตฌ๋งค๊ธ์ก</div>
<div class="col-1">[[${#numbers.formatCurrency(finalPrice)}]]</div>
</div>
<div class="row mb-3">
<div class="offset-5 col-1 d-grid">
<input type="button" class="btn btn-primary" value="์ ํ๊ตฌ๋งค">
</div>
<div class="col-1 d-grid">
<input type="button" class="btn btn-primary" value="์ ํ์ญ์ ">
</div>
</div>
</div>
</div>
</th:block>
<!-- ์ฐ๊ฒฐํด์ผ ํ ์ธ๋ถ js ํ์ผ-->
<th:block layout:fragment="content_js">
<!--<script type="text/javascript" src="/js/test.js(๋ณ๊ฒฝ)"></script> --> <!-- ์ธ๋ถ js ์ฐ๊ฒฐํ๋ ์ฝ๋๋ฅผ block ์์ ์์ฑ layout์์ ๊ฐ์ด ์ด์ด์ค -->
</th:block>
</html>
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ผํ๋ชฐ ํ๋ก์ ํธ(11) ํ์ MY PAGE - ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก (2) (0) | 2023.04.07 |
---|---|
json ๋ฐ์ดํฐ java๋ก ๋ณด๋ด๊ธฐ (0) | 2023.04.07 |
์ผํ๋ชฐ ํ๋ก์ ํธ(10) ์ฅ๋ฐ๊ตฌ๋ (0) | 2023.04.04 |
์ผํ๋ชฐ ํ๋ก์ ํธ(10) ์ํ ๋ชฉ๋ก ํ๋ฉด (0) | 2023.04.04 |
์ผํ๋ชฐ ํ๋ก์ ํธ(9) - ์ํ ๊ด๋ฆฌ (๊ฒ์ ๊ธฐ๋ฅ) (0) | 2023.04.03 |