저번엔 pom.xml을 봤는데.. 이번엔 web.xml이다. context.xml도 알아보겠다.
가장 먼저 web.xml이란 Deployment Descriptor(배포 서술자)로 각 어플리케이션의 환경을 설정하는 부분을 담당한다.
뭐 간단하게.. 설명하면 설정을 위한 설정파일이라는거?
최초로 WAS(Tomcat)이 구동될 때, 각종 설정을 정의해주는 것이다.
WAR 파일이 패키지 될 때 같이 포함되며(pom에서 packing 설정 가능했었음) root directory 밑에 /WEB-INF 디렉토리에 위치한다. 일단 .. 하나의 프로젝트에는 무조건! 하나의 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</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> <!-- 모든 요청을 DispatcherServlet이 처리한다. -->
</servlet-mapping>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
뭐 본인의 web.xml은 이렇다. 차근차근 봐보자.
(1) <context-param>
STS에서 기본적으로 제공해주는 설정 파일외에, 사용자가가 직접 컨트롤 하는 XML파일을 지정해주는 역할을 한다.
'
(2) <listener>
스프링 설정 정보를 읽어주는 역할을 한다.
(3) <servlet>
전에 포스팅에서도 말했듯이, servlet-mapping을 통해 모든 요청을 받아들이고(본인은 모든 요청을 받게 설정),
servlet-class 를 사용하여 dispatcher가 모든 요청을 받아들이는 역할을 담당할 것이며, param-value를 통해 dispatcher-serlvet의 설정 값을 넣을 수 있다.(~~~식으로 진행해 라는 꼴)
(4) <filter>
- 한글 깨짐문제를 CharacterEncoding을 사용하여 인코딩 해준다.
<url-pattern>/*</url-pattern> -> / 이하의 모든 요청에 대해 설정 적용
또한 본인은 설정하지 않았지만 추가적인 태그도 있다.
ㅁ web.xml 제목과 설명을 넣을 수 있는 태그
- <display-name>, <description>
ㅁ 세션을 설정할 수 있는 태그
- <session-config>
ㅁ error page 설정할 수 있는 태그(오류가 발생한다면 error 페이지를 보이게 할 수 있음)
- <error-page>
ㅁ 첫화면 설정, 시작페이지 설정(주의할 점은 컨트롤러에 /가 있더라도 welcom-file-list가 우선순위다)
- <welcome-file-list>
그 다음 root-context.xml이다.
root-context.xml는 웹어플리케이션에서 비즈니스 목적을 위해 존재한다.
일단 소스를 봐보자.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<util:properties id="properties" location="classpath:properties-local/config.local.properties"/>
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 오라클 접속 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"/>
<property name="url" value="jdbc:log4jdbc:oracle:thin:@localhost:1521/xe"/>
<property name="username" value="**"/>
<property name="password" value="**"/>
</bean>
<!-- Mybatis 연동 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:mappers/**/**/*sql.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
뭐 이런식으로 되어있다. 보면 오라클을 접속하고, Mybats 연동하고.. 뭐이런코드가 들어있지않은가?
그래서 비즈니스 로직을 위한 xml파일이라고 생각하면 편하다.
즉, 데이터베이스를 처리하고 db에 관한 bean들에 대한 주 설정이 이루어진다고 생각하면 된다.
오늘까지 해서 xml 정리는 끝난것 같다.
다음엔 뭐하지.. 뭐 하려고 했는데 기억이 안난다.
'Java Spring' 카테고리의 다른 글
[Java Spring] Controller-Service(impl)-DAO(impl)-Mybatis (0) | 2021.04.09 |
---|---|
[Java Spring] Servlet 에 관해서 (0) | 2021.04.08 |
[Java Spring] pom.xml (0) | 2021.04.04 |
[Java Spring] dispatcher-servlet과 MVC2 Model (0) | 2021.04.04 |
[Java Spring] web.xml과 url-pattern의 의미 등 (0) | 2021.04.04 |