최신 글
8
제목 게시일
27
Web

[Spring] DispatcherServlet을 알아보자

profile
코우
2021-08-02 01:25
조회 수 : 4848

DispatcherServlet은 Spring MVC 패턴에서 Front Controller를 담당하는 매우 중요한 컴포넌트 중 하나이다.

이번 포스팅에서는 이 중요한 역할을 하는 DispatcherServlet에 대해 알아볼 것이다.



Front Controller


Front Controller 패턴은 Spring MVC구조에서 함께 사용되는 디자인 패턴이다. 
그렇다면 Front Controller는 무엇일까?

reference


그림에서 나타나는 것과 같이 Front Controller는 웹 애플리케이션으로 들어오는 모든 요청을 수신하고 각 요청에 알맞는 controller로 요청을 넘기는 역할을 한다.

Front Controller가 존재하지 않는다면 각 요청에 맞는 각각의 서블릿을 구성해야 하는 번거로움이 있다. 이렇게 되면 관리해야할 서블릿의 수가 엄청나게 많아지며 공통 코드의 중복 또한 대량으로 발생한다. 


Front Controller가 주는 이점

  1. 공통 코드의 중복을 최소화

  2. 개발 및 유지보수 용이

  3. 서블릿으로부터의 탈출 -> controller는 servlet을 사용할 필요가 없어졌다.


DispatcherServlet


Front Controller에 대해 알았으니 이제 DispatcherServlet을 사용하는 방법에 대해서 알아보자.


web.xml 등록

DispatcherServlet은 다른 서블릿과 마찬가지로 배치 기술자(web.xml) 파일 내에 기술되어야 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
cs

위에 작성된 코드에 따라 모든 요청을 DispatcherServlet이 받게된다.
DispatcherServlet이 받게되는 url 패턴은 url-pattern에 정의한다.

DispatcherServlet이 로드되면 contextConfigLocation 파라미터로 설정 파일이 지정되어 있지 않다면 WEB-INF/서블릿이름-servlet.xml파일에 정의된 빈들을 WebApplicationContext에 로딩한다


DispatcherServlet 설정 파일

DispatcherServlet 설정 파일에는 주로 HandlerMapping, Controller, ViewResolver, View 등의 빈을 설정하게 된다.

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
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <annotation-driven /> // HandlerMapping과 HandlerAdapter를 Bean 등록
    
    
    <context:component-scan base-package="com.example.controller" /> // Controller 스캔 및 Bean 등록
    
    
    <resources mapping="/resources/**" location="/resources/" /> // 정적 자원 경로 매핑 ex) css,javascript 파일
 
    
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> // ViewResolver 설정 및 Bean 등록
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>    
    
</beans:beans>
cs


 
share
twitter facebook kakao naver
댓글