DispatcherServlet
DispatcherServlet은 내부적으로 스프링 컨테이너를 생성한다. 별도의 초기화 파라미터 없이 DispatcherServlet을 설정하면, 웹 어플리케이션의 /WEB-INF/ 디렉토리에 위치한 [서블릿이름]-servlet.xml 파일을 스프링 설정 파일로 사용한다. 예를 들어, 다음과 같이 web.xml 파일을 설정했다면, DispatcherServlet은 dispatcher-servlet.xml 파일을 이용해서 스프링 컨테이너를 생성한다.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
한 개 이상의 설정 파일을 사용해야 하거나 이름이 [서블릿이름]-servlet.xml 형식이 아닌 파일을 사용해야 한다면, 다음과 같이 contextConfigLocation 초기화 파라미터로 설정 파일 목록을 지정하면 된다.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/main.xml
/WEB-INF/bbs.xml
classpath:/common.xml
</param-value>
</init-param>
</servlet>
contextConfigLocation 파라미터는 콤마, 공백, 탭, 줄 바꿈, 세미콜론을 이용하여 파일을 구분한다. 각 설정 파일은 웹 어플리케이션의 루트 디렉토리를 기준으로 하며, "file:"이나 "classpath:" 접두어를 이용해서 로컬 파일이나 클래스 패스에 위치한 파일을 사용할 수 있다.
XML 설정 파일이 아닌 @Configuration 클래스를 이용해서 설정 정보를 작성했다면, 다음과 같이 contextClass를 추가로 설정해주어야 한다.
<servlet>
<servlet-name>dispatcherConfig</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.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
[패키지경로].MvcConfig
</param-value>
</init-param>
</servlet>
contextClass 값을 지정하지 않으면 XmlWebApplicationContext를 사용하는데, 이 클래스는 XML 설정 파일을 사용한다. 따라서 @Configuration 기반의 자바 설정을 이용하는 경우 AnnotationConfigWebApplcationContext 클래스를 사용하도록 contextClass 값을 지정해주어야 한다.
contextConfigLocation 값은 XML 설정 파일 경로 대신 @Configuration 자바 클래스의 완전한 이름을 지정한다. 마찬가지로 콤마, 공백, 탭, 줄 바꿈, 세미콜론을 이용하여 클래스를 구분한다.
캐릭터 인코딩 필터 설정
스프링은 요청 파라미터의 캐릭터 인코딩을 지정할 수 있는 서블릿 필터 CharacterEncodingFilter를 제공하고 있으므로, 요청 파라미터의 캐릭터 인코딩 처리를 위해 별도의 코드를 작성할 필요가 없다. 다음과 같이 설정만 해주면 된다.
<web-app>
<!-- 생략 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Reference
- 웹 개발자를 위한 Spring 4.0 프로그래밍 (최범균 저)
'Spring' 카테고리의 다른 글
| [Spring] 스프링 MVC(4) - 서블릿 매핑 (0) | 2022.03.12 |
|---|---|
| [Spring] 스프링 MVC(3) - 스프링 MVC 설정 (0) | 2022.03.12 |
| [Spring] 스프링 MVC(1) - 기본 흐름과 구성 요소 (0) | 2022.03.12 |
| [Spring] 스프링 AOP(7) - AspectJ 표현식 (0) | 2022.02.20 |
| [Spring] 스프링 AOP(6) - Proxy Target Class (0) | 2022.02.20 |