백기선(인프런 강의)/스프링 웹 MVC
스프링 부트의 스프링 기본 세팅
레알윙
2020. 8. 28. 23:14
반응형
1. 스프링 부트를 프로젝트를 만들기 전 아래와 같이 Thymeleaf와 Spring Web 의존성을 추가해 준다.
2. 아래의 구조에 맞게 코드를 만들어 준다.
package kr.co.study;
import java.time.LocalDateTime;
public class Event {
private String name;
private int limitofEnrollment;
private LocalDateTime startDateTime;
private LocalDateTime endDateTime;
public Event(String name, int limitofEnrollment, LocalDateTime startDateTime, LocalDateTime endDateTime) {
super();
this.name = name;
this.startDateTime = startDateTime;
this.limitofEnrollment = limitofEnrollment;
this.endDateTime = endDateTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getStartDateTime() {
return startDateTime;
}
public void setStartDateTime(LocalDateTime startDateTime) {
this.startDateTime = startDateTime;
}
public int getLimitofEnrollment() {
return limitofEnrollment;
}
public void setLimitofEnrollment(int limitofEnrollment) {
this.limitofEnrollment = limitofEnrollment;
}
public LocalDateTime getEndDateTime() {
return endDateTime;
}
public void setEndDateTime(LocalDateTime endDateTime) {
this.endDateTime = endDateTime;
}
}
package kr.co.study;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class EventController {
@Autowired
EventService eventService;
@GetMapping("/events")
public String events(Model model) {
model.addAttribute("events", eventService.getEvents());
return "events/list";
}
}
package kr.co.study;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class EventService {
public List<Event> getEvents() {
Event event1 = new Event("스프링 웹 MVC 스터디 1차", 5, LocalDateTime.of(2020, 8, 28, 23, 0),
LocalDateTime.of(2020, 8, 29, 0, 0));
Event event2 = new Event("스프링 웹 MVC 스터디 2차", 5, LocalDateTime.of(2020, 8, 30, 11, 0),
LocalDateTime.of(2020, 8, 30, 12, 0));
return List.of(event1, event2);
}
}
package kr.co.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringMvcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMvcApplication.class, args);
}
}
반응형