이번주는 마이바티스와 스프링 MVC 연동, 스프링 부트 세팅, 마이바티스와 스프링 부트 연동, ajax, 웹소켓 실습을 했다. 수업을 듣다가 '지금 내가 강의장에서 한 세팅을 집에 있는 컴퓨터에서 다시 할 수 있을까'라는 생각이 들었다. '설정이 반'이라는 말이 있듯 스프링에는 다양한 설정 파일들과 설정 옵션들이 있어 나와 같은 초심자에게는 다소 버거웠다. 강의장의 세팅을 집으로 옮겨오는 연습도 할 겸 정리차원에서 기록을 남겨본다.
우선 웹앱이 로딩될 때 이용되는web.xml
부터 보자.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml
classpath:annotation/springmvc/member.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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>
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
스프링에서 모든 요청은 ServletDispatcher 에서 처리한다. 따라서 appServlet
이라는 이름의 DispatcherServlet가 모든 패턴의 url을 처리하도록 설정한다.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
그 다음에 appServlet
이 스프링 프레임워크의 DispatcherServlet이라는 것을 설정을 통해 알려줘야 한다. 그리고 appServlet
이 요청을 핸들링할 때 참고할 설정 파일인 servlet-context.xml
로 알려준다.
<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>
한편, post로 들어오는 요청은 utf-8로 인코딩되어 있는데, 한글 등의 기타 문자가 요청에 포함되어 있는 경우 요청 디코딩을 원활하게 하기 위해 문자 인코딩을 utf-8로 설정한다.
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
이제 DispatcherServlet인 appServlet
이 참조하는 설정파일인 servlet-context.xml
파일을 보자.
<?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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<!-- WEB-INF/views/hello.jsp -->
</beans:bean>
<context:component-scan base-package="annotation.springmvc" />
</beans:beans>
annotation.springmvc
패키지의 annotaion을 스캔하며 스프링이 관리할 빈들을 찾아낸다.
<context:component-scan base-package="annotation.springmvc" />
컨트롤러가 뷰 이름을 문자열로 리턴하면 view resolver가 다음의 규칙을 적용해서 알맞은 view를 찾아낸다. prefix
설정은 뷰 이름 앞에 붙는 경로명이고 suffix
는 뷰 이름 뒤에 붙는 확장자이다. 아래의 설정에서 컨트롤러가 "a"
라는 문자열을 리턴하면 view resolver는 /WEB-INF/views/a.jsp
라는 뷰를 찾는다.
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<!-- WEB-INF/views/hello.jsp -->
</beans:bean>
클라이언트가/reousrces/**
패턴의 요청을 하면 {webroot}/resources/
아래를 찾아본다. 현재 내 프로젝트의 웹루트는 {프로젝트경로}/src/main/webapp
이므로 서버는 {프로젝트경로}/src/main/webapp/resources/
를 찾아볼 것이다.
<resources mapping="/resources/**" location="/resources/" />
——————————————————————————