๐ ์ํ์ฝ๊ธฐ ํ์ด์ง(R)
- ๊ธฐ๋ฅ : ๋ชจ๋ ์ฌ์ฉ์๊ฐ ์ํ์ ์ ๋ณด๋ฅผ ๋ณด๋ ํ์ด์ง
์ด์ ํฌ์คํ ์ ๋ด์ฉ์ธ ์ํ๋ฑ๋ก์ ๋๋ด๋ฉด ์๋์ผ๋ก ์ํ ์ฝ๊ธฐ ํ์ด์ง๋ก ๋์ด์ค๊ธฐ๋ ํ๊ณ ์ฌ์ฉ์๊ฐ ์ํ ๋ชฉ๋ก์์ ์ง์ ์ํ ์ฝ๊ธฐ ํ์ด์ง๋ก ์ ๊ทผํ ์๋ ์๋ค. ๊ทธ๋ ๋ค๋ฉด ์ํ ์ฝ๊ธฐ๋ฅผ ํ ๋ ํ์ํ ๋ฐ์ดํฐ๋ค์ ์๊ฐํด๋ณด๊ณ ๋ง๋ค์ด๋ณด์ !
โ read.html modelAndView๋ก ์ฐ๊ฒฐ
โน๏ธ Controller
โถ๏ธ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋ gid๋ post Write ๋ฉ์๋์์ resposeObject๋ก ๋ณด๋ด์ค goodsIndex ๊ฐ
โถ๏ธ gid ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ํ์ select ํ์ฌ ์ฌ์ฉ์๊ฐ ์ฝ๊ณ ์ถ์ ์ํ์ ์ ํํ๋ค ์ ๋ณด๋ฅผ modelAndView๋ก ๋ณด๋ธ๋ค.
โถ๏ธ ์ ํํ ์ํ์ด ๊ฐ์ง๊ณ ์๋ ์ ๋ณด์ ์๋น์ค์์ ์์ฑํ ๋ฉ์๋๋ฅผ ์ด์ฉํด ์ฝ๊ธฐํ์ด์ง์์ ํ์ํ ์ ๋ณด (category, seller,sizes,colors)๋ฅผ ์ ํ ํ modelAndView๋ก ๋ณด๋ธ๋ค.
@RequestMapping(value = "read",
method = RequestMethod.GET,
produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public ModelAndView getRead(@RequestParam(value = "gid") int gid) {
ModelAndView modelAndView = new ModelAndView("goods/read");
GoodsVo goods = this.goodsService.getItem(gid);
ReviewEntity[] reviews = this.goodsService.getReviews(gid);
int sum = 0;
int reviewCount = reviews.length;
double ScoreAvg = 0;
for (int i = 0; i < reviews.length; i++) {
sum += reviews[i].getScore();
}
if (reviewCount > 0) {
ScoreAvg = (double) sum / reviewCount;
ScoreAvg = (double) (Math.round(ScoreAvg * 10));
ScoreAvg = ScoreAvg / 10;
} else {
ScoreAvg = 0;
}
goods.setScoreAvg(ScoreAvg);
goods.setIndex(gid);
modelAndView.addObject("goods", goods);
modelAndView.addObject("category", this.goodsService.getCategory(goods.getCategoryId()));
modelAndView.addObject("seller", this.goodsService.getBrand(goods.getSellerIndex()));
modelAndView.addObject("sizes", this.goodsService.getItemSize(gid));
modelAndView.addObject("colors", this.goodsService.getItemColors(gid));
return modelAndView;
}
โน๏ธ Service : ๊ฐ ํ ์ด๋ธ์ ๊ธฐ๋ณธํค(index,id)๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ select ํ๋ ์๋น์ค๋ฅผ ์์ฑํ๋ค.
public GoodsVo getItem(int index) {
return this.goodsMapper.selectItemByIndexExceptImage(index);
}
public ItemCategoryEntity getCategory(String id) {
return this.goodsMapper.selectItemCategoryById(id);
}
public SellerEntity getBrand(int index) {
return this.goodsMapper.selectSellerByIndexExceptImage(index);
}
public ItemSizeEntity[] getItemSize(int itemIndex) {
return this.goodsMapper.selectSizeByItemIndex(itemIndex);
}
public ItemColorEntity[] getItemColors(int itemIndex) {
return this.goodsMapper.selectColorsByItemIndex(itemIndex);
}
โน๏ธ HTML
โถ๏ธ ํ์๋ฆฌํ๋ฅผ ์ด์ฉํ์ฌ Controller์์ ๋ณด๋ด์ค ๊ฐ์ ์ด์ฉํด ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ ์ํ์ฝ๊ธฐ ํ์ด์ง๋ฅผ ๋ง๋ ๋ค.
<div class="title_box">
<div class="item-title-img">
<img th:src="@{./titleImage (index=${goods.getIndex()})}"
th:alt="${goods.getTitleImageName()}">
</div>
<div class="item-info" id="itemInfo">
<h2 th:utext="${goods.getItemName()}" style="font-family: none"></h2>
<form class="content" id="form">
<input name="price" type="hidden" th:value="${goods.getPrice()}">
<table class="table" id="table">
<tbody>
<tr class="item-category">
<th>์ํ ๋ถ๋ฅ</th>
<td th:text="${category.getText()}"></td>
</tr>
<tr class="item-brand">
<th>๋ธ๋๋</th>
<td>
<div class="item-content" id="logoContainer">
<img class="logo"
th:src="@{./sellerImage (index=${seller.getIndex()})}"
th:alt="${seller.getImageName()}">
<a th:href="${seller.getHomePage()}"
th:text="${seller.getStoreName()}"
class="brand"></a>
</div>
</td>
</tr>
<tr class="item-price">
<th>๊ฐ๊ฒฉ</th>
<td name="price" class="price"
th:value="${goods.getPrice()}"
th:text="${#numbers.formatInteger(goods.getPrice(), 0, 'COMMA')+'์'}"></td>
</tr>
<tr class="color-option">
<th>์์</th>
<td>
<select id="colorOption" onchange="result()">
<option value="0">์์์ ์ ํํด ์ฃผ์ธ์</option>
<option disabled value="">------------------------</option>
<option th:each="color: ${colors}"
th:value="${color.getItemIndex}"
th:text="${color.getColor()}">
</option>
</select>
</td>
</tr>
<tr class="size-option">
<th>์ฌ์ด์ฆ</th>
<td>
<select id="sizeOption" onchange="result()">
<option value="0">์ฌ์ด์ฆ๋ฅผ ์ ํํด ์ฃผ์ธ์</option>
<option disabled value="">------------------------</option>
<option th:each="size:${sizes}"
th:value="${size.getItemIndex}"
th:text="${size.getSize()}">
</option>
</select>
</td>
</tr>
</tbody>
</table>
</form>
'Project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DIOS] ์ํ ์ญ์ (0) | 2023.06.18 |
---|---|
[DIOS] ์ํ ์์ ํ์ด์ง (0) | 2023.06.18 |
[DIOS] ์ํ ๋ฑ๋ก ํ์ด์ง (2) | 2023.02.25 |
[DIOS] ์ผํ๋ชฐ ๋ฆฌ๋ทฐ < Paging | Pagination > ๊ตฌํ (2) | 2023.01.18 |
[DIOS] ๊ฒ์ํ ๋น๋ฐ๋๊ธ ๊ตฌํ (3) | 2023.01.18 |