๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

PRG(Post/Redirect/Get) ํŒจํ„ด๊ณผ ์˜ˆ์‹œ ๋ณธ๋ฌธ

๐ŸŒฑSPRING

PRG(Post/Redirect/Get) ํŒจํ„ด๊ณผ ์˜ˆ์‹œ

jhj.sharon 2023. 11. 8. 09:48
๋ฐ˜์‘ํ˜•

| PRG ํŒจํ„ด 

์›น ๊ฐœ๋ฐœ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ํผ์„ ์ œ์ถœํ•œ ํ›„ ์ƒˆ๋กœ ๊ณ ์นจ์„ ๋ˆ„๋ฅด๋ฉด ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๋ณต ์ œ์ถœ๋˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ๋™์ผ ๊ฒŒ์‹œ๋ฌผ์ด๋‚˜ ๊ฐ™์€ ์ฃผ๋ฌธ์ด ๋ฐ˜๋ณต๋˜์–ด ์ด๋ฃจ์–ด ์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋กœ ๋™์ž‘ํ•œ๋‹ค.

 

1. POST: ์‚ฌ์šฉ์ž๊ฐ€ ์›น ์–‘์‹์„ ์ž‘์„ฑํ•˜๊ณ  ์ œ์ถœํ•˜๋ฉด, ๋ฐ์ดํ„ฐ๋Š” POST ์š”์ฒญ์„ ํ†ตํ•ด ์„œ๋ฒ„๋กœ ์ „์†ก๋œ๋‹ค.
2. Redirect: ์„œ๋ฒ„๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„, ์‚ฌ์šฉ์ž๋ฅผ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ(์˜ˆ: ์„ฑ๊ณต ํŽ˜์ด์ง€, ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€ ๋“ฑ)ํ•œ๋‹ค. ์ด๋•Œ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋Š” GET ์š”์ฒญ์„ ์‚ฌ์šฉํ•œ๋‹ค.
3. GET: ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ์ง€์‹œ์— ๋”ฐ๋ผ GET ์š”์ฒญ์„ ํ•˜์—ฌ ์ƒˆ ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•œ๋‹ค.

 

์ด ํŒจํ„ด์˜ ํ•ต์‹ฌ์€ POST ์š”์ฒญ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•œ ์งํ›„์— ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๋”๋ผ๋„ GET ์š”์ฒญ๋งŒ ๋‹ค์‹œ ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๋ณต์œผ๋กœ ์„œ๋ฒ„์— ์ „์†ก๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๊ฐœ์„ ํ•˜๊ณ , ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

| PRG ํŒจํ„ด ์˜ˆ์‹œ - ๊ฒŒ์‹œํŒ ๊ธ€ ๋“ฑ๋ก

์•„๋ž˜ ์˜ˆ์‹œ๋Š” ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ ์‹ ๊ทœ ๊ณต์ง€์‚ฌํ•ญ ๋“ฑ๋ก๊ธฐ๋Šฅ์— ๊ด€ํ•œ ๊ฒƒ์ด๋‹ค.

 

1. ์‚ฌ์šฉ์ž๋Š” ์•„๋ž˜ JSP์—์„œ ๊ฒŒ์‹œ๊ธ€์„ ๊ด€๋ จ ํ•ญ๋ชฉ์„ ์ž…๋ ฅํ•˜๊ณ  ๋“ฑ๋กํ•œ๋‹ค. '์ €์žฅ'๋ฒ„ํŠผ ํด๋ฆญ์‹œ javascript ํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•˜๋ฉด์„œ ์ปจํŠธ๋กค๋Ÿฌ์— POST ์š”์ฒญ์ด ์ „๋‹ฌ๋œ๋‹ค.

 

<section id="container">
    <div class="contents">
        <h2>๊ณต์ง€์‚ฌํ•ญ ${subTitle}</h2>

        <form name="insertNoticeBoardOne" method="post" enctype="multipart/form-data">
            <input type="hidden" name="noticeBoardSubType" value="${noticeBoardSubType}"/>
            <input type="hidden" id="editmode" name="editmode" value="${editmode}"/>
            
            <div class="inner">
                <div class="inputWrap">
                    <div class="gridWrap">
                        <!-- ์ œ๋ชฉ ์ž…๋ ฅ ํ•„๋“œ -->
                        <div>
                            <span>์ œ๋ชฉ</span>
                            <input type="text" id="bbsTtl" name="noticeTitle" maxlength="200"/>
                        </div>
                        <!-- ์ž‘์„ฑ์ž ์ •๋ณด  -->
                        <div>
                            <span>์ž‘์„ฑ์ž</span>
                           
                        </div>
                        <!-- ์ž‘์„ฑ์ผ ์ž…๋ ฅ ํ•„๋“œ -->
                        <div>
                            <span>์ž‘์„ฑ์ผ</span>
                            <div>
                                <input type="text" name="noticeFirstRegistPtm" readonly="readonly" class="no-border"/>
                            </div>
                        </div>
                        <!-- ๋‚ด์šฉ ์ž…๋ ฅ ํ•„๋“œ -->
                        <div>
                            <span>๋‚ด์šฉ</span>
                            <textarea id="bbsCn" name="bbsCn" rows="10" cols="40" maxlength="4000"></textarea>
                        </div>
                        <!-- ํŒŒ์ผ ์ฒจ๋ถ€ ํ•„๋“œ -->
                        <div class="file-upload">
                            <span>ํŒŒ์ผ์ฒจ๋ถ€</span>
                            <div id="file_upload_posbl" class="board_attach2">
                                <input name="file_1" id="egovComFileUploader" type="file" multiple />
                                <div id="egovComFileList"></div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- ๋ฒ„ํŠผ ์˜์—ญ -->
                <div class="btnArea">
                    <ul>
                        <li>
                            <button type="button" onClick="goList()">๋ชฉ๋ก</button>
                        </li>
                        <li>
                            <button type="button" onClick="editData()">์ €์žฅ</button>
                        </li>
                    </ul>
                </div>
            </div>
        </form>
    </div>
</section>

 

2. POST : ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋Š” ์›น์—์„œ ์š”์ฒญ๋ฐ›์€ insertAdminNoticeBoard๋ฉ”์„œ๋“œ๋Š” ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์„œ๋ฒ„์— ์ €์žฅ์ด ๋๋‚œ ๋’ค ๋ชฉ๋ก ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•œ๋‹ค.

 

3. GET: ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ์ง€์‹œ์— ๋”ฐ๋ผ GET ์š”์ฒญ์„ ํ•˜์—ฌ ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•œ๋‹ค.

 

    @PostMapping(value = "/insertAdminNoticeBoard.do")
    public String insertAdminNoticeBoard(final MultipartHttpServletRequest multiRequest, NoticeBoardVO boardVO, ModelMap model) throws Exception {
        List<FileVO> result = null;
        String atchFileId = "";
        boardVO.setNoticeBoardType(4);

        final Map<String, MultipartFile> files = multiRequest.getFileMap();
        if (!files.isEmpty()) {
            result = fileUtil.parseFileInf(files, "BBS_", 0, "", "");
            atchFileId = fileMngService.insertFileInfs(result);
            boardVO.setAtchFileId(atchFileId);
        }
     
        announcementAdminSerivce.insertBoard(boardVO);

        model.addAttribute("editmode", "I");
        model.addAttribute("boardVO", boardVO);

        return "redirect:admin/selectAminNoticeBoardList.admin";
    }

    @GetMapping({"/Announcement.do","/Board.do", "/QnA.do"})
    public String selectAminNoticeBoardList(@ModelAttribute("searchVO") NoticeBoardVO searchVO,  HttpServletRequest request, ModelMap model){



        /* pageing setting */
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
        paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
        paginationInfo.setPageSize(searchVO.getPageSize());

        searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
        searchVO.setLastIndex(paginationInfo.getLastRecordIndex());
        searchVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());


        List<NoticeBoardVO> boardList = announcementAdminSerivce.selectBoardList(searchVO);

        int totCnt = announcementAdminSerivce.selectBoardListTotCnt(searchVO);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("boardList", boardList);

        model.addAttribute("noticeBoardSubType", searchVO.getNoticeBoardSubType());

        model.addAttribute("paginationInfo", paginationInfo);



        return "admin/AdminNoticeBoardList.admin";
    }
๋ฐ˜์‘ํ˜•
Comments