0. pom.xml
- Maven을 사용한 라이브러리 및 플러그인 추가 등에 사용
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kr.or</groupId>
<artifactId>ddit</artifactId>
<name>springProj</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.2.5.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
1. web.xml
src/main/webapp/WEB-INF/spring/web.xml
- 설정을 위한 설정 파일
- Tomcat과 같은 WAS(Web Application Server)가 최초로 구동될 때 실행
- root-context.xml의 경로 지정
- DispatcherServlet을 정의하고 서블릿 설정 파일인 servlet-context.xml 파일의 경로를 설정
- 그 외 <filter>, <error-page> 설정
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2. root-context.xml
src/main/webapp/WEB-INF/spring/root-context.xml
- 프로젝트 전체의 다른 웹 컴포넌트 간에 공유할 설정을 관리
- 주로 Back-End의 설정을 담당
- 클래스 빈 생성 등
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="SPRING" />
<property name="password" value="java" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:/sqlmap/**/*_SQL.xml" />
<property name="configLocation" value="/WEB-INF/mybatisAlias/mybatisAlias.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
3. servlet-context.xml
src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
- 서블릿 컨테이너의 초기화를 위한 설정 파일
- 주로 Front-End의 설정을 담당
- ViewResolver 설정
- 정적 리소스(css, js 등)의 경로 설정
- HandlerInterceptor 빈 생성 및 경로 설정
<?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">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<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="kr.or.ddit" />
<beans:bean id="loginInterceptor" class="kr.or.ddit.interceptor.LoginInterceptor" />
<interceptors>
<interceptor>
<mapping path="/member/withdrawal" />
<mapping path="/member/update" />
<mapping path="/member/logout" />
<beans:ref bean="loginInterceptor" />
</interceptor>
</interceptors>
</beans:beans>
annotation-driven | |
resource | css, images, js 등의 파일이 저장될 경로를 설정 |
InternalResourceViewResolver | ModelAndView에서 setViewName()으로 경로를 설정할 때 입력받은 경로의 접두사와 접미사로 붙을 문자열을 설정 예) setViewName("main") -> /WEB-INF/views/main.jsp |
context:component-scan | 어노테이션으로 클래스 빈을 생성할 패키지 경로를 설정 |
interceptors | 인터셉터를 적용할 경로와 HandlerInterceptor 클래스 설정 |
'Java > Spring Framework' 카테고리의 다른 글
[Spring Framework] 파일 업로드 (0) | 2023.01.23 |
---|---|
[Spring Framework] Interceptor로 로그인 여부 확인하기 (0) | 2023.01.22 |
[Spring Framework] 핵심 개념 (0) | 2023.01.20 |
[Spring Framework] MyBatis 연동하기 (0) | 2023.01.19 |
[Spring Framework] Eclipse 스프링 STS 설정하기 (0) | 2023.01.19 |