본문 바로가기

Spring/Spring

[Spring] 타임리프 - 리터럴

반응형

타임리프에서 리터럴을 사용할 때를 보겠습니다.

타임리프에서 리터럴은 아래와 같습니다.

문자 : 'hello', 'world'

숫자 : 10 20 30

boolean : true, false

null : null

문자는 작은 따옴표로 감싸야 하는데 이는 상당히 귀찮습니다. 그래서 띄어쓰기가 없다면 그냥 하나의 토큰으로 보고 작은 따옴표가 아닌 " "로 감싸도 됩니다.

< th:text="hello">  -- O

<th:text="hello world"> -- X

이 경우는 공백이 있기 때문에 작음따옴표로 감싸주어야 합니다.

<th:text=" 'hello world' "> -- O

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<h1>리터럴</h1>
<ul>
  <li>'hello' + ' world!' = <span th:text="'hello' + ' world!'"></span></li>
  <li>'hello world!' = <span th:text="'hello world!'"></span></li>
  <li>'hello ' + ${data} = <span th:text="'hello ' + ${data}"></span></li>
  <li>리터럴 대체 |hello ${data}| = <span th:text="|hello ${data}|"></span></li>
</ul>
</body>
</html>

 

리터럴 대체는 | |로 리터럴을 감싸는 리터럴 대체 문법인데 1,2,3번째 코드보다 훨씬 편리한 것을 볼 수 있습니다.

반응형