> For the complete documentation index, see [llms.txt](https://docs.sysout.co.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sysout.co.kr/web/back-end/what-is-spring-framework/spring-mvc-project/servlet-context-xml.md).

# servlet-context.xml

## 파일 전체 내용 보기

```markup
<?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" />
    </beans:bean>

    <context:component-scan base-package="com.hakademy.spring06" />



</beans:beans>
```

## 파일 내용 상세 설명

### xml 헤더

xml 문서임을 명시하는 xml 헤더 코드이다. 모든 xml 문서의 첫 번째 줄에 위치한다.

```markup
<?xml version="1.0" encoding="UTF-8"?>
```

### 기본 영역 설정

servlet-context.xml의 기본 설정 영역이다.

```markup
<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">

</beans:beans>
```

* xmlns : 기본 XML Namespace 선언. 이 주소의 내용을 기본 태그로 사용 가능
* xmlns:beans : beans XML Namespace 선언. beans: 형태의 태그 사용 가능
* xmlns:context : context XML Namespace 선언. context: 형태의 태그 사용 가능
* xmlns:xsi : XML Schema Instance 스펙 정보 선언
* xsi:schemaLocation : 실제 선언된 정보들의 위치를 알려주는 역할

즉, 이 문서에서는 위의 내용에 의해 다음과 같은 규칙이 적용된다.

* 아무런 표시 없이 태그를 사용하면 spring-mvc 모듈의 내용을 사용할 수 있다.
* beans:로 시작하는 태그를 사용하면 spring-beans 모듈의 내용을 사용할 수 있다.
* context:로 시작하는 태그를 사용하면 spring-context 모듈의 내용을 사용할 수 있다.

### MVC Annotation 활성화

아래 코드를 통해 @Controller와 같은 mvc annotation을 활성화한다.

```markup
<annotation-driven />
```

### 정적 자원 설정

DispatcherServlet에서 알아야 할 정적 자원들의 위치와 주소를 설정한다.

* css
* js
* image
* font

이외에도 다양한 형태의 정적 자원들이 프로젝트 내에 존재할 수 있으므로 연결 설정을 해주면 DispatcherServlet에서 이 설정을 제외한 나머지(/)를 처리한다.

```markup
<resources mapping="/resources/**" location="/resources/" />
```

### View Resolver 설정

다음은 View Resolver 설정 코드이다.

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

View Resolver는 컨트롤러에서 일을 좀 더 쉽게 처리할 수 있도록 도와주는 도구이다. 아직 컨트롤러를 배우지 않았기 때문에 간략하게 설명한다면 View Resolver의 역할은 다음과 같다.

* "은평구" 라고 말하면 자동으로 "서울시"를 앞에 붙여준다.
* "갤럭시" 라고 말하면 자동으로 "삼성"을 앞에 붙이고 "휴대폰"을 뒤에 붙여준다.

View Resolver는 앞/뒤에 자동으로 JSP 경로에 필요한 값들을 붙여주는 역할을 수행하며, 앞에 붙이는 것을 접두사(Prefix), 뒤에 붙이는 것을 접미사(Suffix)라 부른다.

만약 컨트롤러에서 "test"를 반환한다면, 설정에 의해 접두사/접미사가 추가되어 다음 형태가 된다.

/WEB-INF/views/test.jsp

### Component Scan 설정

Spring MVC Project에서는 자동화를 위해 기본적으로 base-package가 component-scan 설정이 되어 있다.

```markup
<context:component-scan base-package="com.hacademy.spring01" />
```

위의 설정은 <mark style="color:blue;">`com.hacademy.spring01`</mark> 패키지를 기준으로 이하 모든 패키지에서 annotation으로 설정된 객체들을 탐색하겠다는 의미이며 이로 인해 좀 더 편리하게 스프링 객체들을 관리할 수 있게 된다.

필요하다면 기본 제공되는 component-scan 필터를 사용하지 않고 수동으로 원하는 항목들을 불러오도록 세부 설정을 할 수 있다.
