스프링 MVC 2편(백엔드 웹 개발 활용 기술)

Ch01. 타임리프(기본 기능) - 리터럴

webmaster 2022. 3. 9. 13:31
728x90
  • 타임리프는 다음과 같은 리터럴이 있다.
    • 문자: 'hello'
    • 숫자: 10
    • 불린: true , false
    • null: null
  • 타임리프에서 문자 리터럴은 항상 ' (작은따옴표)로 감싸야한다.
    • 공백 없이 쭉 이어진다면 하나의 의미 있는 토큰으로 인지해서 다음과 같이 작은따옴표를 생략할 수 있다.
  • <span th:text="hello world!"></span>
    • 문자 리터럴은 원칙상 ' 로 감싸야한다. 중간에 공백이 있어서 하나의 의미 있는 토큰으로도 인식되지 않는다
@GetMapping("/literal")
public String literal(Model model){
    model.addAttribute("data", "Spring!");
    return "basic/literal";
}
<!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 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>
728x90