Spring Boot

[ Spring Boot ] 스프링MVC & Thymeleaf part.2

hanjuCoding 2024. 7. 30. 14:19

Thymeleaf

 

타임리프는 계속 된다.....

 

반복문에 state (index) 처리

<ul>
    <li th:each="dto ,state : ${list}">
        [[${state.index}]] --- [[${dto}]]
    </li>
</ul>

 

제어문

th:if ~ unless문

    <ul>
        <li th:each="dto ,state : ${list}" th:if="${dto.sno%5==0}">
            [[${state.index}]] --- [[${dto}]]
        </li>
    </ul>

 


inline 속성

SampleController일부

    @GetMapping("/exInline")
    public String exInline(RedirectAttributes redirectAttributes) {
        log.info("exInline......");

        SampleDTO dto = SampleDTO.builder()
                .sno(100L)
                .first("First..100")
                .last("Last..100")
                .regTime(LocalDate.from(LocalDateTime.now()))
                .build();
        redirectAttributes.addFlashAttribute("result", "succes");
        redirectAttributes.addFlashAttribute("dto", dto);
        return "redirect:/sample/ex3";
    }

    @GetMapping("/ex3")
    public void ex3(){
        log.info("ex3");
    }

 

ex3 일부

    <h1 th:text="${result}"></h1>
    <h1 th:text="${dto}"></h1>
    <script th:inline="javascript">
        var msg = [[${result}]];
        var dto = [[${dto}]];
    </script>

    <!--inline안썼을때-->
    <script>
        var msg = [[${result}]];
        var dto = [[${dto}]];
    </script>

인라인을 안쓰면 객체가 제대로 JSON형태로 출력되지만, 사용하지 않으면 객체로 그냥 출력됨

 


+) 추가

태그로 생성되지 않는 th:block, 링크처리 th:href는 어렵지 않으니 알아두기