ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 03/23(Tue) findById์™€ window.location.search
    HangHae99/TIL-hanghae99 2021. 3. 23. 22:30

    ๐Ÿšฉ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ์ œ์™ธํ•œ CRUD๋ฅผ ๊ตฌํ˜„ํ•œ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

     

     

    ์–ด์ œ ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜ ๊ณ„์† ํ•ด๊ฒฐ์ด ์•ˆ๋๋˜ ๋ฌธ์ œ 2๊ฐ€์ง€. ํ•˜๋ฃจ ์˜จ์ข…์ผ ํ•ด๊ฒฐ์„ ๋ชปํ•ด์„œ ๊ฒฐ๊ตญ ๋ฉ˜ํ† ๋‹˜์ด ํ•ด๊ฒฐํ•ด์ฃผ์…จ๋‹ค.

    ๋ชจ๋ฅด๋Š” ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ๋ง‰๋ง‰ํ•˜๊ณ , ๋‹ต๋‹ตํ•˜๋‹ค. ์–ด์ œ๋Š” ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ๋Š”๋ฐ ์ž˜ ๋„˜์–ด๊ฐ”๊ณ  ํž˜๋“ค๊ฒŒ ์–ป์–ด๋‚ธ ๊ฒƒ ์ •๋ฆฌ ์ž˜ํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž.

     

     

    ๐Ÿ“• ํด๋” ๊ตฌ์„ฑ

     

    ๋ฉ”์ธ์ด ๋˜๋Š” ํŽ˜์ด์ง€๋Š” index.html 

    ๊ธ€์„ ์ž‘์„ฑํ•˜๋Š” ํŽ˜์ด์ง€๋Š” write.html  

    ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ์กฐํšŒ ํŽ˜์ด์ง€๋Š” detail.html

     

     

    index.html์€ ์ „์ฒด ๊ฒŒ์‹œ๊ธ€์ด ๋ณด์—ฌ์ง€๊ณ , ๊ทธ ์ค‘ ํ•œ ๊ฒŒ์‹œ๊ธ€์˜ ์ œ๋ชฉ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ๋Š” detail.html๋กœ ๋„˜์–ด๊ฐ€์„œ ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ, ์ž‘์„ฑ์ž, ์ž‘์„ฑ์ผ, ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

    index.html์—์„œ ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” write.html๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

     

     

    html ํŽ˜์ด์ง€ ์†Œ๊ฐœ

     

    ๐Ÿ“‘ ์ฝ”๋“œ ํ๋ฆ„

    1. ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ write.html๋กœ ํŽ˜์ด์ง€๊ฐ€ ๋„˜์–ด๊ฐ„๋‹ค.

     

    <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item active write" aria-current="page"><a href="write.html" class="write-link">write</a></li>
            </ol>
        </nav>

     

    2. write.html์—์„œ write๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด writeBoard()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    write.html

    <div id="btn-done">
         <button type="submit" onclick="writeBoard()" class="btn btn-primary">write</button>
    </div>

     

    2-1. writeBoard()ํ•จ์ˆ˜

     

    function writeBoard() {
                let title = $('#inputTitle').val();
                let username = $('#inputUsername').val();
                let contents = $('#inputContents').val();
                
                let data = {'title':title, 'username':username, 'contents':contents};
                
                $.ajax({
                    type: "POST",
                    url: "/api/boards",
                    
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (response) {
                        alert("๊ธ€์ด ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        window.location.href="index.html";
                    }
    
                })
    
            }

    ๊ฐ inputํƒœ๊ทธ๋“ค์˜ value๊ฐ’์„ ๋ณ€์ˆ˜์— ๋‹ด์•„๋‘๊ณ  ์ด๋“ค์„ data ๋ณ€์ˆ˜์— JSONํ˜•์‹์œผ๋กœ ๋‹ด์•„๋‘”๋‹ค.

    ์ปจํŠธ๋กค๋Ÿฌ์— ์š”์ฒญํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ธ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์ด ์˜ค๋ฉด index.html๋กœ ์ด๋™ํ•˜๊ฒŒ ํ•œ๋‹ค.

     

     

    3. index.html -> ํŠน์ •๊ฒŒ์‹œ๊ธ€ ํด๋ฆญ -> detail.html ์ด๋™

     

    index.html

     

    index.html์—์„œ 'ํ—ฌ๋กœ์›”๋“œ!' ์ œ๋ชฉ์„ ํด๋ฆญํ•˜๋ฉด

     

     

    detail.html

     

    ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ์„ ํด๋ฆญํ•˜๋ฉด detail.html๋กœ ์ด๋™ํ•˜๊ฒŒ ํ•ด๋’€๋Š”๋ฐ, ์ด ๋•Œ url์— id๋ฅผ ๊ฐ™์ด ์ฟผ๋ฆฌ๋กœ ๋„˜๊ธด๋‹ค.

     

    function addHTML(id, username, title, contents, createdAt) {
                console.log("addHTMlํŽ‘์…˜์— ๋“ค์–ด์™”์Œ.")
                
                let tempHtml = `<div class="list_content">
                                    <a  class="title-edit" href="detail.html?id=${id}" onclick="showOneList('${id}'>
                                        <strong id="${id}-title">${title}</strong>
                                    </a>
                                    <div class="info-edit">
                                        <p class="info-username" id="${id}-username">${username}</p>  |
                                        <span id="${id}-time">${createdAt}</span>
                                    </div>
                                </div>`
                $('#list-box').append(tempHtml);
    
            }

     

     

     

    4. ์ด ๋ชจ๋“  ๊ณ ๋‚œ์ด ์—ฌ๊ธฐ์„œ ์‹œ์ž‘๋˜์—ˆ๋‹ค. detail.html

     

    1) ๊ณ ๋‚œ 1

    url๋กœ id๋ฅผ ๋„˜๊ฒจ ์คฌ๋Š”๋ฐ, url์—์„œ ์ด id๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅธ๋‹ค.

    ๋ฐฉ๋ฒ•์„ ์ฐพ๋‹ค๊ฐ€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ, a ํƒœ๊ทธ์— showOneList(id)๋กœ๋„ id๋ฅผ ๋„˜๊ฒจ๊ฐ€๋ฉฐ ์ด๊ฒƒ์ €๊ฒƒ ๊ณ ์ณ๋ดค๋Š”๋ฐ, ์–ด๋–ป๊ฒŒ ๋„˜๊ธฐ๋”๋ผ๊ณ  id ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ๋ชป ์ฐพ์•˜์—ˆ๋‹ค.

     

    ๋‹ค์‹œ url๋กœ ๋„˜๊ธด id ๊ฐ€์ ธ์˜ค๋Š” ๋ฒ•์„ ์ฝ”๋“œ๋กœ ๋ณด์ž.

     

    $(document).ready(function() {
                $('#boards-box').empty();
    
                let id = window.location.search.split('=')[1];  //?id=2
                // "?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8"
                console.log(id);    //์ž˜ ๋‚˜์˜ค๊ณ 
                showOneList(id);
            })

    detail.html์— ๋“ค์–ด์™”์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋“ค.

     

    ๊ทธ๋ฆฌ๊ณ  url๋กœ ๋„˜๊ธด id๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ - window.location.search

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด "?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8" ์ด๋Ÿฐ์‹์œผ๋กœ url๋’ค์— ์ฟผ๋ฆฌ๋กœ ๋„˜๊ธด ๊ฒƒ๋“ค์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

    ๋‚ด ์ฝ”๋“œ์—์„  id=2๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‚˜๋Š” ์•„์ด๋”” ๊ฐ’๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ ' = '๋ฅผ ๊ธฐ์ค€์œผ๋กœ split()๋ฅผ ํ•˜๊ณ  ์ธ๋ฑ์Šค[1]์„ ๊ฐ€์ ธ์˜ค๋ฉด ์•„์ด๋”” ๊ฐ’๋งŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฑธ let id ๋ณ€์ˆ˜์— ๋‹ด๊ณ  showOneList(id)์— id๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.

     

     

    2) ๊ณ ๋‚œ 2

     

    showOneList(id) ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž.

    function showOneList(id){
                // id ์ž˜ ๋ฐ›์•„์™”๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.
                console.log(`id: ${id}`)
                $.ajax({
                    type: 'GET',
                    url: `/api/boards/${id}`,
                    success: function (resp) {
                        addBoardHTML(
                            resp.title,
                            resp.username,
                            resp.createdAt,
                            resp.contents
                        );
                    }
                })
            }

     

    ์ปจํŠธ๋กค๋Ÿฌ์— id์™€ ๊ฐ™์ด ์š”์ฒญ์„ ํ–ˆ๋Š”๋ฐ, ์ด๊ฒƒ๊ณผ ๋งค์น˜๋˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    @GetMapping("/api/boards/{id}")
        public Board getOneBoard(@PathVariable Long id) {
            return boardService.readOne(id);
        }

     

     

    ์„œ๋น„์Šค์—์„œ readOne(id)ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ์œผ๋ฏ€๋กœ ์„œ๋น„์Šค ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

    @Transactional(readOnly = true)
        public Board readOne(Long id) {
            log.info("id: {}", id);
            final Board board = boardRepository.findById(id).orElseThrow(
                    () -> new IllegalArgumentException("ํ•ด๋‹น ์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.")
            );
    
            log.info("board: {}", board);
            return board;
        }

     

    ์ด ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์•ˆ๋๋‹ค. 

    final Board board = boardRepository.findById(id).orElseThrow(
                    () -> new IllegalArgumentException("ํ•ด๋‹น ์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.")
    );

    repository์—์„œ find by id๋ฅผ ํ•˜๋ฉด ๊ฒฐ๊ณผ ๊ฐ’์ด id ๊ฐ’ ํ•˜๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์ด๊ฑธ board์— ๋‹ด์œผ๋ฉด, board์— ์žˆ๋Š” id ๋ฉค๋ฒ„๋ณ€์ˆ˜๋งŒ ์„ค์ • ๋˜๋Š” ๊ฑฐ ์•„๋‹Œ๊ฐ€ ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜๋‹ˆ, return ํ•ด์„œ board๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ดค์ž ์ปจํŠธ๋กค๋Ÿฌ๋Š” id๋งŒ ์„ค์ •๋ผ์žˆ๋Š” board๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๊ณ , ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ด board๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์‘๋‹ตํ•ด์ค„ํ…๋ฐ ์–ด๋–ป๊ฒŒ ๊ฒฐ๊ณผ๊ฐ’์ด ์ž˜ ์ฐํžˆ๋Š” ๊ฑด์ง€ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค.

     

    ์ด ๋ถ€๋ถ„์„ ํŒ€์›๋“ค๊ป˜ ์—ฌ์ญค๋ดค๋”๋‹ˆ, findById๋ฅผ ํ•˜๋ฉด id๋งŒ ์ฃผ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๊ทธ ์•„์ด๋””์™€ ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ•œ ํ–‰์„ ๋ฆฌํ„ดํ•ด ์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•˜์…จ๋‹ค. ์˜ค๋ธŒ์ ํŠธ ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜์ด ๋œ๋‹ค๊ณ  ํ•˜์…จ๋Š”๋ฐ ๊ทธ๊ฑด ์•„์ง ๋ฌด์Šจ ์˜๋ฏผ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์ฐพ์•„๋ด์•ผ์ง€.

    ์ง„์ž‘์— ํŒ€์›๋“คํ•œํ…Œ ์—ฌ์ญค๋ณผ ๊ฑธ...

     

    ๋‹ค์‹œ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€์„œ log.info๋ฅผ ๋ณด๋ฉด board์— ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด๊ฐ€์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฑธ ํ™•์ธํ•ด๋ณด์ž.

     

     

    log.info("board: {}", board);

    log.infoํ•œ ๋ชจ์Šต

    ์œ„์—์„œ ์˜ˆ์‹œ๋กœ ๋“  'ํ—ฌ๋กœ์›”๋“œ!' ๊ฒŒ์‹œ๊ธ€์„ ํด๋ฆญํ–ˆ์„ ๋•Œ board์— ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’.

    ์ด๋ ‡๊ฒŒ ๋ˆˆ์œผ๋กœ ํ™•์ธํ•˜๋‹ˆ๊นŒ ์ดํ•ด๊ฐ€ ์ž˜ ๋œ๋‹ค.

     

     

    ๋Œ“๊ธ€