리터럴

Mina Park·2022년 11월 17일
0
  1. 기본개념
  • 소스코드상에 고정된 값
  • 타임리프의 리터럴
    • 문자, 숫자, 불린, null
  1. 사용방법
  • 무조건 문자 리터럴은 작은 따옴표로 감싸야 함
<span th:text="'hello'">
  • 단, 공백 없이 쭉 이어질 경우에는 하나의 의미있는 토큰으로 인지하므로 작은 따옴표 생략 가능
    • 룰: A-Z , a-z , 0-9 , [] , . , - , _
<span th:text="hello">
  1. 적용예시
  @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>

0개의 댓글