Spring

Spring Boot STEP 4 - Thymeleaf2

2023. 6. 28. 12:22
728x90

★ 타임리프 연산자

  • 산술 연산자 > 동일
<div th:text="${a} + ${b}"></div>
<div th:text="${a} - ${b}"></div>
<div th:text="${a} * ${b}"></div>
<div th:text="${a} / ${b}"></div>
<div th:text="${a} % ${b}"></div>
  • 비교 연산자 > 동일 + 영문 표현 제공
<div th:text="${a} <= ${b}"></div>
<div th:text="${a} == ${b}"></div>
<div th:text="${a} != ${b}"></div>
  • 논리 연산자 > 동일 + 영문 표현 제공
<div th:text="${a > 5} and {b < 10}"></div>
<div th:text="${a > 5} or {b < 10}"></div>
  • 삼항 연산자 > 동일 
<div th:text="${c} != null ? ${c} : '데이터 없음'"></div>
<div th:text="${c} ? ${c} : '데이터 없음'"></div>
<div th:text="${c} ?: '데이터 없음'"></div> <!-- Elvis 연산자 -->
<div th:text="${c} ?: _">데이터 없음</div> <!-- No-Operation -->
  • 문자열 연산자 > 동일

 

★ HTML 속성 조작

  • th:HTML속성명="값"
  • 기존에 동일한 속성이 선언되어 있으면 대체한다.
  • 기존에 동일한 속성이 선언되어 있지 않으면 추가한다.
  • 대부분 서버에서 전달 받은 값(model)은 
- 직접 표현식으로 아무곳에서 사용이 불가능(${key})
- 표현식은 반드시 th:속성에만 적용이 가능
  • 사용 ex)
<div>${name}</div>
<div th:text="${name}"></div>

<input type="text" name="age">
<input type="text" th:name="age">
<input type="text" name="${name}">
<input type="text" th:name="${name}">
<input type="text" th:name="${name}" th:size="${size}">
<input type="text" th:value="${color}">

<hr>

<div class="one">Box 1</div>	 
<div th:class="one">Box 2</div>	 
<div class="one" th:class="two ">Box 3</div>

<div class="one" th:attrappend="class=' two'">Box 4</div>
<div class="one" th:attrprepend="class='two '">Box 5</div>

<div class="one" th:classappend="two">Box 6</div>

<input type="checkbox" name="cb" th:checked="true">
<input type="checkbox" name="cb" th:checked="false">


<!-- <div style="background-color:${color}">Box 7</div> -->
<div th:style="'background-color:' + ${color}">Box 7</div>
<div th:style="|background-color:${color}|">Box 7</div>

 

★ PCDATA 조작

  • HTML 콘텐츠 영역 > 데이터 출력
1. th:text
    - escaped text
    - '<' -> &lt;
    - '>' -> &gt;

2. th:utext
    - unescaped text
    - 그대로 출력
    
<코드>
<div th:text="${txt}"></div>
<div th:utext="${txt}"></div>
  • 인라인 출력
1. th:inline="text" // 사용 잘 안함
2. th:inline="javascript" //많이 사용(필수)

1. th:inline + [[]] //escaped text
2. th:inline + [()] //unescaped text

<코드>
<div th:text="${txt}"></div>
	 <div th:utext="${txt}"></div>
	 
	 <hr>
	 
	 <div th:inline="text">[[${name}]]</div>
	 <div th:inline="text">[(${name})]</div>
	 
	 <div th:inline="text">[[${txt}]]</div>
	 <div th:inline="text">[(${txt})]</div>
	 
	 <div>[[${txt}]]</div>
	 
	 <!--/*
	 <div th:inline="text">[[${a}]] + [[${b}]] = [[${a + b}]]</div>
	 */-->
	 
	 <hr>
	 
	 <div id="label1"></div>
	 <div id="label2"></div>
	 
<script>

	let name1 = '[[${name}]]';
	let num1 = [[${num}]];
	let names1 = '[[${names}]]';
	let dto1 = '[[${dto}]]';

	document.getElementById('label1').textContent = name1;
	
</script>
<script th:inline="javascript">

	let name2 = [[${name}]];
	let num2 = [[${num}]];
	let names2 = [[${names}]];
	let dto1 = [[${dto}]];

	document.getElementById('label2').textContent = name2;
	
</script>

 

★ 데이터를 일정한 형식으로 표현하는 도구

  • #numbers
  • #dates
<h2>숫자</h2>

<div th:text="${num1}"></div>
<div th:text="${#numbers.formatInteger(num1, 3, 'COMMA')}"></div>

<div th:text="${num2}"></div>
<div th:text="${#numbers.formatDecimal(num2, 3, 'COMMA', 2, 'POINT')}"></div>

<hr>

<h2>날짜</h2>

<div th:text="${#dates.year(now)}"></div>
<div th:text="${#dates.month(now)}"></div>
<div th:text="${#dates.monthName(now)}"></div>
<div th:text="${#dates.monthNameShort(now)}"></div>
<div th:text="${#dates.day(now)}"></div>
<div th:text="${#dates.hour(now)}"></div>
<div th:text="${#dates.minute(now)}"></div>
<div th:text="${#dates.second(now)}"></div>
<div th:text="${#dates.millisecond(now)}"></div>
<div th:text="${#dates.dayOfWeek(now)}"></div>
<div th:text="${#dates.dayOfWeekName(now)}"></div>
<div th:text="${#dates.dayOfWeekNameShort(now)}"></div>

<div th:text="${#dates.format(now)}"></div>
<div th:text="${#dates.format(now, 'yyyy-MM-dd HH:mm:ss')}"></div>
<div th:text="${#dates.format(now, 'yyyy-MM-dd aa hh:mm:ss')}"></div>

 

★ 링크 주소 만들기

Link URL Expressions
- @{}
- 링크의 URL 표현
a. 매개변수 처리 쉽다.
b. Context Root Path가 자동으로 삽입
  • 사용 ex)
<div><a href="/m7">이전 페이지</a></div>
<div><a href="/spring/m7">이전 페이지</a></div>

<div><a th:href="@{/m7}">이전 페이지</a></div>

<hr>

<h3>QueryString, 매개변수</h3>
<div><a href="/m7?seq=100">이전 페이지</a></div>
<div><a href="/m7?seq=${seq}">이전 페이지</a></div>
<div><a th:href="@{/m7(seq=${seq})}">이전 페이지</a></div>

<div><a href="/m7?seq=100&mode=add">이전 페이지</a></div>
<div><a th:href="@{/m7(seq=${seq}, mode=${mode})}">이전 페이지</a></div>
728x90
저작자표시 비영리 변경금지 (새창열림)

'Spring' 카테고리의 다른 글

Spring Boot STEP 6 - JPA  (0) 2023.07.01
Spring Boot STEP 5 - Thymeleaf3  (0) 2023.06.29
Spring Boot STEP 3 - Thymeleaf  (0) 2023.06.27
Spring Boot STEP 2 - 기본적인 CRUD 사용  (0) 2023.06.26
Spring Boot STEP 1 - 기초 셋팅 및 실행  (0) 2023.06.26
'Spring' 카테고리의 다른 글
  • Spring Boot STEP 6 - JPA
  • Spring Boot STEP 5 - Thymeleaf3
  • Spring Boot STEP 3 - Thymeleaf
  • Spring Boot STEP 2 - 기본적인 CRUD 사용
IT의 큰손
IT의 큰손
IT계의 큰손이 되고 싶은 개린이의 Log 일지
IT의 큰손
Developer Story House
IT의 큰손
전체
오늘
어제
  • 분류 전체보기 (457)
    • 정보처리기사 필기 (18)
    • 정보처리기사 실기 (12)
    • 정보처리기사 통합 QUIZ (12)
    • 빅데이터 (11)
    • 안드로이드 (11)
    • 웹페이지 (108)
    • 자바 (49)
    • SQLD (3)
    • 백준 알고리즘 (76)
    • 데이터베이스 (41)
    • 깃허브 (2)
    • Library (14)
    • Server (31)
    • 크롤링&스크래핑 (3)
    • Spring (23)
    • Vue.js (13)
    • React (27)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Developer Stroy House

인기 글

태그

  • 프론트엔드
  • it
  • jquery
  • java
  • 코딩테스트
  • 백준
  • JavaScript
  • 알고리즘
  • DB
  • 웹페이지
  • 개발블로그
  • 자바
  • 웹개발자
  • css
  • 데이터베이스
  • DBA
  • 백엔드
  • IT자격증
  • html
  • 개발자
  • 앱개발자
  • jsp
  • IT개발자
  • 정보처리기사
  • ajax
  • React
  • 정보보안전문가
  • 정보처리기사필기
  • 웹개발
  • IT자격증공부

최근 댓글

최근 글

Designed By hELLO
IT의 큰손
Spring Boot STEP 4 - Thymeleaf2
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.