Spring Web MVC web.xml 분석

2022. 1. 30. 16:13북리뷰/토비의 봄

728x90

web.xml이란?

web.xml은 DD(Deploymeet Descriptor, 배포 설명자)이며, Web Application의 설정 파일이다.

즉, 웹 어플리케이션을 실행시킬 때 함께 올라가야할 설정을 정의해놓은 것이다.

 

 

web.xml 구성

web.xml은 크게 DispatcherServlet, ContextLoaderListener, Filter로 나뉜다.

 

DispatcherServlet

HTTP 프로토콜로 매핑되는 요청을 받아 적합한 컨트롤러에게 위임해주는 역할을 한다.

어플리케이션으로 들어오는 모든 요청을 핸들링하고 공통 작업을 처리한다.

클라이언트 요청을 다음과 같은 순서로 처리한다.

1. 클라이언트 요청을 처리해줄 컨트롤러 탐색 (HandlerMapping 객체가 처리)

2. 컨트롤러 실행(HandlerAdapter 객체가 처리)

3. 클라이언트에게 보여질 View 탐색(ViewResolver가 처리)

4. 응답 데이터와 view를 클라이언트에게 전달

Controllers, ViewResolver, HandlerMapplig을 생성하는 Servlet WebApplicationContext를 생성한다.

 

ContextLoaderListener

각 DispatcherServlet이 공통으로 사용할 빈들을 정의해 놓은 root-context이다.

즉, 모든 Servlet이 참조가 가능한 부모 Context이다.

Services와 Repositories를 생성하는 Root WebApplicationContext를 생성한다.

 

Filter

클라이언트에서 온 요청을 DispatcherServlet이 받기 전/후에 실행한다.

용자 요청에 대한 정보를 검사하는 역할을 한다.

 

예시

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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/dispatcher-servlet.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>

    <!-- Encoding Filter 생성 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

applicationContext.xml (ContextLoaderListener)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="SimpleDriverDataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/toby"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>
</beans>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:component-scan base-package="com.tutorial"/>
</beans>

 

 

출처: https://tlatmsrud.tistory.com/35

728x90