Templete Engine중 하나인 Thymeleaf 사용기

전에는 Handlebars를 사용해 봤는데 이번엔 좀 다른걸 시도했다. (왜냐하면 spring이 밀고 있다고 해서)

  • 변수 다루는 방법

    • 스프링에서 넘어온 변수(Model또는 ModelAndView)를 html문서 상 표현식

      1
      ex) $String name = "seunghwan" -> ${name} = seunghwan, todoList={1,2,3,4,5,...} -> ${todoList} = [1,2,3,4,5,...]
    • 변수 값을 주소값에 담는 방식

      1
      <button type="submit" th:formmethod="post" th:formaction="@{/delete/{index}(index=${b.index})}">삭제</button>

      결과 :

    • 변수 값을 폼에 담는 방식

      1
      <button type="submit" th:formmethod="post" th:formaction="@{/delete/index(index=${b.index})}">삭제</button>

      결과 :
      post방식으로 하면 원래 주소창에 안보이지만 그래도 결과값을 알기 위해서 표시해봄

언제 타임리프 태그인 <th:> 붙여야 하는지 모르겠다. 그냥 필요할때만 하면 될까?


참고 링크
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax
https://indra818.github.io/2017/11/23/thymeleaf-tag-reference/