Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

hy6

2-16 공통 템플릿 (실습 기록) 본문

점프 투 스프링 부트

2-16 공통 템플릿 (실습 기록)

rantinum 2023. 10. 26. 13:14

1. 오류 메시지 공통 템플릿

  • 오류 메시지를 출력하는 공통적인 템플릿을 제작하자

  • form_errors.html

    <div th:fragment="formErrorsFragment" class="alert alert-danger" 
     role="alert" th:if="${#fields.hasAnyErrors()}">
     <div th:each="err : ${#fields.allErrors()}" th:text="${err}" />
    </div>

    2. 질문 등록 템플릿에 적용하기

  • 만들어둔 템플릿을 질문 등록하는 템플릿에도 적용해보자

  • question_form.html

    <html layout:decorate="~{layout}">
    <div layout:fragment="content" class="container">
     <h5 class="my-3 border-bottom pb-2">질문등록</h5>
     <form th:action="@{/question/create}" th:object="${questionForm}" method="post">
         <div th:replace="~{form_errors :: formErrorsFragment}"></div>
       ( 생략 ) 
     </form>
    </div>
    </html>

    3. 질문 상세 템플릿에 적용하기

  • question_detail.html

    <html layout:decorate="~{layout}">
    <div layout:fragment="content" class="container my-3">
     (... 생략 ...)
     <!-- 답변 작성 -->
     <form th:action="@{|/answer/create/${question.id}|}" th:object="${answerForm}" method="post" class="my-3">
         <div th:replace="~{form_errors :: formErrorsFragment}"></div>
         <textarea th:field="*{content}" rows="10" class="form-control"></textarea>
         <input type="submit" value="답변등록" class="btn btn-primary my-2">
     </form>
    </div>
    </html>