1. pom.xml 설정
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. applicatoin.properties 설정
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.view-names=thymeleaf/*
3. vo 설정
package com.kangong.sample.model;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @Setter
@Builder @AllArgsConstructor @NoArgsConstructor
public class Event {
private String name;
private int limitOfEnrollment;
private LocalDateTime startDateTime;
private LocalDateTime endDateTime;
}
4. Service 설정
package com.kangong.sample.service;
import java.time.LocalDateTime;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kangong.sample.model.Event;
@Service
public class SampleService {
@Autowired
private SqlSession sqlSession;
public String gettNow() {
return sqlSession.selectOne("kangong.test.selectNow");
}
public List<Event> getEvents(){
Event event = Event.builder()
.name("스프링 웹 MVC 스터디 1차")
.limitOfEnrollment(5)
.startDateTime(LocalDateTime.of(2020, 12, 6, 12 , 47))
.endDateTime(LocalDateTime.of(2020, 12, 6, 15 , 47)) .build();
Event event2 = Event.builder()
.name("스프링 웹 MVC 스터디 2차")
.limitOfEnrollment(5)
.startDateTime(LocalDateTime.of(2020, 12, 10, 12 , 47))
.endDateTime(LocalDateTime.of(2020, 12, 10, 15 , 47)) .build();
return List.of(event, event2);
}
}
5. Controller 설정
@GetMapping("/events")
public String events(Model model) {
System.out.println("evnents");
model.addAttribute("events",sampleService.getEvents());
return "thymeleaf/events";
}
}
6. html 설정
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<TITLE>Events</TITLE>
</head>
<body>
<h1>이벤트 목록</h1>
<table>
<tr>
<th>이름</th>
<th>참가 인원</th>
<th>시작</th>
<th>종료</th>
</tr>
<tr th:each="event: ${events}">
<td th:text="${event.name}">이벤트 이름</td>
<td th:text="${event.limitOfEnrollment}">100</td>
<td th:text="${event.startDateTime}">2020년 12월 2일</td>
<td th:text="${event.endDateTime}">2020년 12월 12일</td>
</tr>
</table>
</body>
</html>
'개발 > Spring Boot' 카테고리의 다른 글
8. Spring Security 설정 (0) | 2021.02.12 |
---|---|
7.aop 설정 (0) | 2021.02.11 |
5. Lombok 설정 (0) | 2021.02.11 |
4. log4j2 설정 (0) | 2021.02.07 |
3. jsp 연동 (0) | 2021.02.07 |