티스토리 뷰

반응형

스프링 부트를 활용한 MVC 동작

  • SpringBoot Main
@SpringBootApplication
public class DemoSpringMvcApplication {

    public static void main(String [] args) {
        SpringApplication.run(DemoSpringMvcApplication.class);
    }
}
  • Controller
@Controller
public class EventController {

    @Autowired
    EventService eventService;

    @GetMapping("/events")
    public String events(Model model) {
        model.addAttribute("events", eventService.getEvent());
        return "events";
    }

}
  • View
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</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년 2월 5일 오전 10시</td>
            <td th:text="${event.endDateTime}">2020년 2월 5일 오전 10시</td>
        </tr>
    </table>
</body>
</html>
  • 스프링 부트 기반의 웹 MVC는 서블릿 기반으로 동작하며, 서블릿 컨테이너가 필요하는 것을 살펴보았다.

 

 

스프링 부트를 사용하지 않는 스프링 MVC

  • 서블릿을 활용한 웹 MVC를 작성하여 스프링 부트의 동작원리를 파악해보았다.

https://it-mesung.tistory.com/79?category=830536

 

[스프링 웹 MVC] 스프링 IoC 컨테이너와 연동

서블릿 애플리케이션 스프링 연동하기 지금까지 만든 Servlet을 활용하여 스프링이 제공하는 IoC 컨테이너와 연동해보자. 서블릿에서 스프링이 제공하는 IoC 컨테이너 활용하는 방법 애플리케이션 컨텍스트를 서블..

it-mesung.tistory.com

https://it-mesung.tistory.com/114?category=830536

 

[스프링 웹 MVC] 스프링 MVC 연동(Dispatcher Servlet)

DispatcherServlet 사용 @RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("/hello") public String hello() { return "hello, " + helloService.getName(); }..

it-mesung.tistory.com

  • 해당 부분은 서블릿 컨테이너에 등록한 웹 애플리케이션에 DispatcherServlet을 등록했다.
    • web.xml에 서블릿 등록
    • Java를 통해 서블릿 등록

애플리케이션 컨텍스트 설정파일

@Configuration
@ComponentScan
public class WebConfig {
  @Bean
  public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }
}

 

web.xml에 서블릿 등록

<servlet>
  <servlet-name>app</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContex</param-value>
  </init-param>
  <init-param>
    <param-name>contextConfigLoaction</param-name>
    <param-value>com.mesung.WebConfig</param-value>
  </init-param>
</servlet>

<servlet>
  <servlet-name>app</servlet-name>
  <servlet-class>/app/*</servlet-class>
</servlet>

 

Java를 통해 서블릿 등록

public class WebApplication implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(WebConfig.class);
    context.refresh();

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    ServletRegistration.Dynamic app = servletContext.addServlet("app", dispatcherServlet);
    app.addMapping("/app/*");
  }
}

 

 

스프링 부트를 사용하는 스프링 MVC

  • 자바 애플리케이션에 내장 톰켓을 만들고 그 안에 DispatcherServlet을 등록한다.
    • 스프링 부트의 자동 설정이 자동으로 해준다.
  • 스프링 부트의 주관에 따라 여러 인터페이스 구현체를 빈으로 등록한다.
    • DispatcherServlet에 관련된 인터페이스의 구현체가 대부분 빈으로 등록되어 있다.
    • 이로 인해 스프링 MVC를 좀 더 편리하게 사용할 수 있는 것이다.

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함