본문 바로가기
Spring/Thymeleaf

th:text 예제

by YoonJong 2022. 9. 13.
728x90

데이터를 주고 받을 때는 Entity 클래스 자체를 반환하면 안되며,

데이터 전달용 객체(Data Tranfer Object)를 생성해서 사용해야 한다.

 

ItemDto.class

@Data
public class ItemDto {
    private Long id;
    private String itemNm;
    private Integer price;
    private Integer stock;
    private String itemDetail;
    private String sellStatus;
    private LocalDateTime regTime;
    private LocalDateTime updateTime;
}

 

컨트롤러 예제

@GetMapping("/ex02")
public String thymeleafExample02(Model model) {
    ItemDto itemDto = new ItemDto();
    itemDto.setItemNm("테스트 상품1");
    itemDto.setItemDetail("상품 상세 설명");
    itemDto.setPrice(10000);
    itemDto.setRegTime(LocalDateTime.now());

    model.addAttribute("itemDto", itemDto);

    return "thymeleafEx/thymeleafEx02";
}

model 에 담은 data 를 사용하는 것을 확인할 수 있다.

 

 

타임리프 예제

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>상품 데이터 출력 예제</h1>
    <div>
        상품명: <span th:text="${itemDto.itemNm}"></span>
    </div>
    <div>
        상품상세설명: <span th:text="${itemDto.itemDetail}"></span>
    </div>
    <div>
        상품등록일: <span th:text="${itemDto.regTime}"></span>
    </div>
    <div>
        상품가격: <span th:text="${itemDto.price}"></span>
    </div>
</body>
</html>

 

결과

728x90

'Spring > Thymeleaf' 카테고리의 다른 글

th:href 예제  (0) 2022.09.13
th:switch & th:case 예제  (0) 2022.09.13
th:if & th:unless 예제  (0) 2022.09.13
th:each 예제  (0) 2022.09.13
thymeleaf 소개, 시작해보기  (0) 2022.09.13

댓글