[DIOS] ์ƒํ’ˆ ์ฝ๊ธฐ ํŽ˜์ด์ง€

๐Ÿ“Œ  ์ƒํ’ˆ์ฝ๊ธฐ ํŽ˜์ด์ง€(R)

- ๊ธฐ๋Šฅ :  ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ’ˆ์˜ ์ •๋ณด๋ฅผ ๋ณด๋Š” ํŽ˜์ด์ง€

 

 

[DIOS] ์ƒํ’ˆ ๋“ฑ๋ก ํŽ˜์ด์ง€

๐Ÿ“Œ ์ƒํ’ˆ ๋“ฑ๋ก(C) - ์กฐ๊ฑด : ๊ด€๋ฆฌ์ž๋งŒ ์ž‘์„ฑ๊ฐ€๋Šฅ - ๊ธฐ๋Šฅ : ์‡ผํ•‘๋ชฐ์— ํŒ๋งคํ•  ์ƒํ’ˆ์„ ๋“ฑ๋ก โœ… DB๊ตฌ์„ฑ ์ƒํ’ˆ์— ํ•„์š”ํ•œ ์ •๋ณด๋“ค์€ ์ƒํ’ˆ๋ช…(item_name),์ƒํ’ˆ์ƒ์„ธ(item_detail),๊ฐ€๊ฒฉ(price), ์ˆ˜๋Ÿ‰(count), ์ž‘์„ฑ๋‚ ์งœ(created_on

o-yoon.tistory.com

์ด์ „ ํฌ์ŠคํŒ…์˜ ๋‚ด์šฉ์ธ ์ƒํ’ˆ๋“ฑ๋ก์„ ๋๋‚ด๋ฉด ์ž๋™์œผ๋กœ ์ƒํ’ˆ ์ฝ๊ธฐ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด์˜ค๊ธฐ๋„ ํ•˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ’ˆ ๋ชฉ๋ก์—์„œ ์ง์ ‘ ์ƒํ’ˆ ์ฝ๊ธฐ ํŽ˜์ด์ง€๋กœ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ƒํ’ˆ ์ฝ๊ธฐ๋ฅผ ํ• ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์ƒ๊ฐํ•ด๋ณด๊ณ  ๋งŒ๋“ค์–ด๋ณด์ž !

 


โœ…  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>