記錄

Spring) ContextLoaderListener, 한글처리(EncodingFilter) 본문

Web/Spring framework

Spring) ContextLoaderListener, 한글처리(EncodingFilter)

surhommejk 2018. 5. 1. 09:53

org.springframework.web.context

Interface WebApplicationContext


public interface WebApplicationContext

extends ApplicationContext



Interface to provide configuration for a web application. This is read-only while the application is running, but may be reloaded if the implementation supports this.

This interface adds a getServletContext() method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process.


Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.


In addition to standard application context lifecycle capabilities, WebApplicationContext implementations need to detect ServletContextAware beans and invoke the setServletContext method accordingly.


org.springframework.web.context

Class ContextLoader


public class ContextLoader

extends java.lang.Object


Performs the actual initialization work for the root application context. Called by ContextLoaderListener.

Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type, falling back to XmlWebApplicationContext if not found. With the default ContextLoader implementation, any context class specified needs to implement the ConfigurableWebApplicationContext interface.


Processes a "contextConfigLocation" context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g. "WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml". Ant-style path patterns are supported as well, e.g. "WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml". If not explicitly specified, the context implementation is supposed to use a default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").


Note: In case of multiple config locations, later bean definitions will override ones defined in previously loaded files, at least when using one of Spring's default ApplicationContext implementations. This can be leveraged to deliberately override certain bean definitions via an extra XML file.


Above and beyond loading the root application context, this class can optionally load or obtain and hook up a shared parent context to the root application context. See the loadParentContext(ServletContext) method for more information.


As of Spring 3.1, ContextLoader supports injecting the root web application context via the ContextLoader(WebApplicationContext) constructor, allowing for programmatic configuration in Servlet 3.0+ environments. See WebApplicationInitializer for usage examples.


org.springframework.web.context

Class ContextLoader


public class ContextLoader

extends java.lang.Object

Performs the actual initialization work for the root application context. Called by ContextLoaderListener.

Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type, falling back to XmlWebApplicationContext if not found. With the default ContextLoader implementation, any context class specified needs to implement the ConfigurableWebApplicationContext interface.


Processes a "contextConfigLocation" context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g. "WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml". Ant-style path patterns are supported as well, e.g. "WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml". If not explicitly specified, the context implementation is supposed to use a default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").


Note: In case of multiple config locations, later bean definitions will override ones defined in previously loaded files, at least when using one of Spring's default ApplicationContext implementations. This can be leveraged to deliberately override certain bean definitions via an extra XML file.


Above and beyond loading the root application context, this class can optionally load or obtain and hook up a shared parent context to the root application context. See the loadParentContext(ServletContext) method for more information.


As of Spring 3.1, ContextLoader supports injecting the root web application context via the ContextLoader(WebApplicationContext) constructor, allowing for programmatic configuration in Servlet 3.0+ environments. See WebApplicationInitializer for usage examples.



<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
      /WEB-INF/applicationContext.xml
   </param-value>
</context-param>



※ param-name은 org.springframework.web.context.ContextLoaderListener에서

이미 설정되어 있는 변수명이므로 contextConfigLocation에서 절대로 바뀌어선 안된다(정해져 있다는 뜻)


※ param-value에서 root application context을 설정하며 복수의 root application context을 설정할 수 있다





*****************************************************************************************************




org.springframework.web.filter

Class CharacterEncodingFilter


public class CharacterEncodingFilter

extends OncePerRequestFilter


Servlet Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form.

This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true"). In the latter case, the encoding will also be applied as default response encoding (although this will usually be overridden by a full content type set in the view).


<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>


※ filter-name은 임의, filter-class는 정확한 자원 위치를 지정하여야 한다

※ param-name은 설정된 자원의 명칭 이므로 'encoding'을 준수해야 한다.

   param-value에는 원하는 encoding type을 지정한다.

※ filter-mapping에서 위에서 정한 filter-name을 정한 후 이 filter가 encoding할 url-pattern을 설정한다



Comments