티스토리 뷰
Config...
Spring Framework를 배우면서 설정 부분을 가장 많이 헤맸다.
그중에서도 web.xml과 XXX-servlet.xml의 연관관계는 아직도 헷갈린다.
그래서 이번 기회에 깊이 파고들어서 정리를 해보려고 한다.
web.xml 파헤치기
web.xml: Deployment Descriptor (배포 서술자)
web.xml의 Element에 대해선 Servlet 기술 문서, Servlet 3.0 pdf의
Section '14.4 Deployment Descriptor Diagram' 에서 찾아 볼 수 있었고,
자주 사용하는 Element 위주로 적어보려고 한다.
<web-app> Element
web.xml의 Root-Element 기능을 담당한다.
- xmlns와 servlet의 버전을 설정
1 2 3 4 5 6 7 | // Servlet 3.0 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee version="3.0"> </web-app> | cs |
<display-name> Element
쉽게 말해 web.xml(DD) 파일의 title을 설정하는 부분이다.
기본적으로 프로젝트명으로 설정된다.
1 | <display-name>viii</display-name> | cs |
<welcome-file-list> Element
서버로 요청이 들어왔을 때 표시할 welcome-file을 순서대로 정의하는 부분이다.
- welcome-file: 적용할 welcome-file의 이름
1 2 3 4 5 | // index.jsp, index.html의 순서로 호출하고, 없다면 404 페이지를 호출한다. <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> | cs |
<error-page> Element
error code 혹은 exception type을 error page로 매핑하기 위한 부분이다.
- error-code: 웹의 에러 코드를 적어주는 변수
- exception-type: java exception type을 적어주는 변수
- location: 매핑할 페이지의 경로를 적는 변수
1 2 3 4 5 6 7 8 | <error-page> <error-code>404</error-code> <location>/error_page/404.jsp</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error_page/404.jsp</location> </error-page> | cs |
<context-param> Element
Servlet context의 parameter를 선언해주는 부분이다.
쉽게 생각하자면 web.xml의 전역 변수 같은 느낌이다.
참고로, <init-param>은 지역변수라고 생각하면 이해가 쉽다.
- param-name: context parameter의 이름
- param-value: context parameter의 값
1 2 3 4 5 | // 대표적으로 spring context 변수를 선언하는 예시 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/spring-context.xml</param-value> </context-param> | cs |
<listener> Element
Application Listener Bean을 가리키기 위한 부분으로,
이때 해당 Bean은 웹 애플리케이션에 등록이 되어있어야 한다.
- listener-class: listener bean의 class를 지정해주는 부분
1 2 3 4 | // spring에서 contextConfigLocation을 설정하기 위한 listener bean 지정 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> | cs |
<filter> Element
웹 애플리케이션에서 사용하는 filter를 선언하는 부분이다.
어떤 filter를 사용할 것인지 설정하는 부분으로, 대표적으로 encodingFilter가 있다.
- filter-name: Required, unique, filter-mapping Element와 매핑하기 위한 변수
- filter-class: 사용할 클래스의 정규화 된 이름을 나타내는 변수
- init-param: filter에서 사용할 name-value 쌍을 선언하는 부분
- async-supported: Optional, 선언된 경우 async 방식으로 사용이 가능
1 2 3 4 5 6 7 8 9 | // Encoding 방식을 UTF-8로 하기 위한 Filter <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> | cs |
<filter-mapping> Element
filter Element가 어떤 filter를 사용할지 설정하는 거였다면,
filter-mapping Element는 설정한 filter를 어디에 적용할 것인지를
URL 또는 Servlet을 통해 선언하는 부분이다.
- filter-name: filter Element와 매핑하기 위한 변수
- url-pattern: filter를 적용할 url을 선언하는 부분
- servlet-name: filter를 적용할 servlet을 선언하는 부분
1 2 3 4 5 | // root 이하 모든 url에 이름이 encodingFilter인 filter를 적용 <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> | cs |
<servlet> Element
Servlet을 선언할 때 사용하는 Element로, 선언에 필요한 정보를 담고 있다.
흔히 spring에서 DispatcherServlet을 선언할 때 자주 볼 수 있다.
- servlet-name: Required, unique, 사용할 servlet의 이름을 지정
- servlet-class: 사용할 클래스의 정규화 된 이름을 나타내는 변수
- init-param: servlet에서 사용할 name-value 쌍을 선언하는 부분
- load-on-startup: servlet이 배치될 때의 우선순위를 지정하기 위한 부분
1 2 3 4 5 6 7 8 9 10 | // 대표적인 DispatcherServlet을 선언하는 예시 <servlet> <servlet-name>dispatcherServlet</servlet-name> <servler-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> | cs |
<servlet-mapping> Element
Servlet과 URL 사이의 매핑을 정의하는 부분이다.
- servlet-name: 매핑할 servlet-name을 선언하는 부분
- url-pattern: servlet과 매핑할 URL 패턴을 선언하는 부분
1 2 3 4 5 | // .viii로 끝나는 모든 URL에 dispatcherServlet을 매핑 <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.viii</url-pattern> </servlet-mapping> | cs |
'dev > java' 카테고리의 다른 글
처음 하는 스프링(Spring), Eclipse 환경 설정 (1) | 2018.06.12 |
---|---|
처음 하는 스프링(Spring), 개발 환경 구성하기 (1) | 2018.06.11 |
설정을 더 간결하게, context:component-scan (0) | 2018.05.28 |
스프링에서의 역할 분담, WebApplicationContext (0) | 2018.03.30 |
프론트 컨트롤러 패턴, Front Controller Pattern (0) | 2018.03.28 |
- Total
- Today
- Yesterday
- 자작냉우동
- Nasdaq
- JMT
- 설정
- 화려해
- MariaDB
- AFK아레나
- 스프링
- mybatis
- 루블
- 블라디보스톡
- 커피프렌즈
- 쏠편한환전
- 맛집
- MVC
- 이클립스
- spring
- 세번가요
- 변수
- sbux
- 주식
- 내돈돌려줘
- 교환코드
- 꿈의숫자
- 스타벅스
- Tomcat
- Java
- 환전
- controller
- 두번가요
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |