[22/1/11] 익숙해지지 않는 타임리프 문법들 정리

Que Lin·2022년 1월 11일
0

1day 1commit

목록 보기
4/63

타임리프 쓸 때 붙여줘야 하는 링크!

<html lang="en" xmlns:th="http://www.thymeleaf.org">

save.html에서 타임리프

 <form action="/member/save" method="post" th:object="${member}">
        <div th:if="${#fields.hasGlobalErrors()}">
            <p class="field-error" th:each="err: ${#fields.globalErrors()}" th:text="${err}">글로벌오류</p>
        </div>
    이메일 : <input type="text" th:field="*{memberEmail}" placeholder="이메일">
    <p th:if="${#fields.hasErrors('memberEmail')}" th:errors="*{memberEmail}" th:errorclass="field-error"></p>

th:object="${member}"

form submit을 할 때, form의 데이터가 th:object에 설정해준 객체로 받아진다.

th:if="${#fields.hasGlobalErrors()}"

조건문. 해당 조건이 만족할 때만 보여준다.

th:each="err:$ {#fields.globalErrors()}", th:each="member:${memberList}"

반복문.

th:text="${err}"

화면에 값을 출력할 때 사용

th:errors="*{memberEmail}"

해당 value의 error가 있는 경우 출력

th:field="*{memberEmail}"

각각 필드들을 매핑해줌. 설정해 준 값으로, th:object에 설정해 준 객체의 내부와 매칭해준다.
name, id 자동 생성

th:errorclass="field-error"

속성 값이 class속성에 설정됨.
필드에 오류가 있을 경우에만 특정 태그를 표시하고 싶을 때
th:if="${#fields.hasErrors('필드이름')"을 설정 -> th:errors="*{필드이름}"을 설정
-> 태그 안에 있는 문자열을 대상 필드에 관련된 에러 메시지로 바꿔 줌

이외

 <input type="hidden" id="memberId" name="memberId" th:value="${member.memberId}">
 
 <input type="button" value="수정(put)" th:onclick="memberUpdate()">

<td><a th:href="@{|/member/${member.memberId}|}">조회</a></td>
            <td><button th:onclick="detail([[${member.memberId}]])">조회(ajax)</button></td>
            <td><a th:href="@{|/member/delete/${member.memberId}|}">삭제(get)</a></td>
            <td><button th:onclick="deleteById([[${member.memberId}]])">삭제(delete)</button>

th:value="${member.memberId}"

타임리프에서 input 태그에 value 를 출력

th:onclick="memberUpdate()"

클릭하여 함수 호출

th:href="@{|/member/${member.memberId}|}"

주소로 보낼 때

세션값 받아올 때 : th:text="${session['loginEmail']}"

문자열 비교할때: th:if="${#strings.equals(session['loginEmail'],'admin')}">

profile
1일 1커밋 1일 1벨로그!

0개의 댓글