◆ DispatcherServlet은 기본적으로 웹 어플리케이션의 /WEB-INF/ 디렉토리에 위치한 [서블릿이름]-servlet.xml 파일로부터 스프링 설정 정보를 읽어 옴

 <servlet>
  <servlet-name>spring303</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>spring303</servlet-name>
  <url-pattern>*.sp</url-pattern>
 </servlet-mapping> 

위와 같이 web.xml 작성 시 스프링 설정 정보의 파일명은 spring303-servlet.xml이 됨

 

 

경우에 따라 한 개 이상의 설정 파일을 사용해야 하는 경우, 혹은 기본 설정 파일 이름이 아닌 다른 이름의 설정 파일을 사용하고 싶은 경우 다음과 같이 사용

  => DispatcherServlet을 설정할 때 contextConfigLocation 초기화 파라미터에 설정 파일 목록을 지정 

 <servlet>
  <servlet-name>spring303</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/main_config.xml

    /WEB-INF/board_config.xml
   </param-value>
  </init-param>
 </servlet>

 
 <servlet-mapping>
  <servlet-name>spring303</servlet-name>
  <url-pattern>*.sp</url-pattern>
 </servlet-mapping>

● contextConfigLocation 초기화 파라미터는 설정 파일 목록을 값으로 가짐

  => 콤마(","), 공백 문자(" "), 탭(\t), 줄 바꿈(\n), 세미콜론(";")을 이용하여 구분

  => 각 설정 파일의 경로는 웹 어플리케이션 루트 디렉토리를 기준으로 함

 

 

◆ ContextLoaderListener : 서로 다른 DispatcherServlet이 공통 빈을 필요로 하는 경우 사용

  => ContextLoaderListener를 ServletListener로 등록하고 contextConfigLocation 컨텍스트 파라미터를 이용하여 공통으로 사용될 빈 정보를 담고 있는 설정 파일 목록을 지정하면 됨

 

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/service.xml
   /WEB-INF/parent_main.xml
   classpath:common.xml
  </param-value>
 </context-param>

 

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

● ContextLoaderListener는 contextConfigLocation 컨텍스트 파라미터를 명시하지 않으면 /WEB-INF/applicationContext.xml을 설정 파일로 사용 

 

 

◆캐릭터 인코딩시 web.xml 파일에 CharacterEncodingFilter 클래스를 설정함으로써 요청 파라미터의 캐릭터 인코딩을 손쉽게 설정할 수 있음

  => 만약 그렇지 않으면 모든 컨트롤러에 response.setCharacterEncoding("UTF-8");를 해줘야 함

 <filter>
  <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범균

+ Recent posts