hades

[Thymeleaf] ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ ์‹œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ ์ฃผ์˜ ๋ณธ๋ฌธ

๐Ÿƒ๐Ÿป‍โ™‚๏ธ ๊ธฐ๋ณธํ›ˆ๋ จ/Spring

[Thymeleaf] ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ ์‹œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ ์ฃผ์˜

hades1 2024. 8. 20. 14:30

๐Ÿšจ ์—๋Ÿฌ

์น˜ํ‚จ3 ๊ฒŒ์‹œ๊ธ€์„ ์ง€์šฐ๊ณ  ์‹ถ์–ด์„œ ์‚ญ์ œ๋ฅผ ๋ˆ„๋ฅด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์น˜ํ‚จ1์ด ์ง€์›Œ์กŒ๋‹ค.

 

action์˜ ๊ฒฝ๋กœ๋„ ์ œ๋Œ€๋กœ ๋œ ๊ฒƒ์„ ํ™•์ธํ–ˆ๋Š”๋ฐ, ์™œ ์ด๋Ÿฐ ์ผ์ด ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ผ๊นŒ?

 

๐Ÿ” ๋ถ„์„

<div class="post" th:each="post : ${posts}" >
    <div th:each="imageFile : ${post.imageFiles}">
        <img class="post-image" th:src="|/post/images/${imageFile.storeFileName}|">
    </div>
    <div class="post-content">
        <div class="post-comment" th:text="${post.comment}"></div>
        <div class="post-date" th:text="|${post.createdDateTime.toLocalDate()} ${post.createdDateTime.getHour()}:${post.createdDateTime.getMinute()}|"></div>

        <form th:action="|/member/mypage/posts/modify/${post.id}|" method="get" id="myPost-modify">
        </form>

        <form th:action="|/member/mypage/posts/delete/${post.id}|" method="post"  id="myPost-delete" onsubmit="return confirm('์ •๋ง ์‚ญ์ œ๋ฅผ ์›ํ•˜์‹ญ๋‹ˆ๊นŒ? ๋˜๋Œ๋ฆด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');">
        </form>

        <button type="submit" form="myPost-modify" class="button-left">์ˆ˜์ •</button>
        <button type="submit" form="myPost-delete" class="button-right">์‚ญ์ œ</button>

    </div>
</div>

๋ฌธ์ œ๋Š” ๊ฒŒ์‹œ๊ธ€์„ ๋‚˜์—ดํ•  ๋•Œ, th:each๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ , ์ˆ˜์ •๊ณผ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ๋ฐฐ์น˜ํ•˜๋Š” ๊ณผ์ •์—์„œ form์— id๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์ด id๊ฐ€ ๊ฒŒ์‹œ๊ธ€๋งˆ๋‹ค ๋ชจ๋‘ ๊ฐ™๊ฒŒ ๋˜์–ด ๋งจ ์•ž์— ์žˆ๋Š” ๊ฒŒ์‹œ๊ธ€์ด ์ง€์›Œ์ง„ ๊ฒƒ์ด์—ˆ๋‹ค.

 

๐Ÿ‘Š ํ•ด๊ฒฐ

<div class="post" th:each="post : ${posts}" >
    <div th:each="imageFile : ${post.imageFiles}">
        <img class="post-image" th:src="|/post/images/${imageFile.storeFileName}|">
    </div>
    <div class="post-content">
        <div class="post-comment" th:text="${post.comment}"></div>
        <div class="post-date" th:text="|${post.createdDateTime.toLocalDate()} ${post.createdDateTime.getHour()}:${post.createdDateTime.getMinute()}|"></div>

        <div class="post-modify">
            <form th:action="|/member/mypage/posts/modify/${post.id}|" method="get">
                <button type="submit">์ˆ˜์ •</button>
            </form>
        </div>

        <div class="post-delete">
            <form th:action="|/member/mypage/posts/delete/${post.id}|" method="post" onsubmit="return confirm('์ •๋ง ์‚ญ์ œ๋ฅผ ์›ํ•˜์‹ญ๋‹ˆ๊นŒ? ๋˜๋Œ๋ฆด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');">
                <button type="submit">์‚ญ์ œ</button>
            </form>
        </div>

    </div>
</div>

button์€ form ์•ˆ์— ๋„ฃ๊ณ , div ํƒœ๊ทธ์™€ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์น˜ํ•˜์˜€๋‹ค.