Certification/Spring(2V0-72.22, SCP)

Spring MVC 웹프로젝트 기본 설정

엘호리스 2018. 8. 5. 13:29

web.xml 파일 : 웹프로젝트의 배치 기술서(deploy descriptor)

웹프로젝트가 로딩될 때 제일 먼저 참조되는 파일. 웹프로젝트의 환경 설정 정보를 저장하고 있는 파일


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <!-- 스프링의 환경설정 파일 로딩 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 위에서 아래로... 제일 먼저 root-context.xml 파일을 로딩한다 -->
        <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>
        <!--  DispatcherServlet이 최초 관문(or 짐검다리) 같은 역할을 한다(내장된 서블릿 클래스) -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--  servlet-context.xml 파일에서 설정을 읽어들여 객체를 생성한다 -->
            <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 패턴을 /로 정하면서 DispatcherServlet이 모든 요청을 가로채게 된다  -->
        <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>
cs



root-context.xml 파일 : 스프링 환경설정 파일. 서블릿 이외 설정. ex) 데이터베이스 연동 설정 등의 정보.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <!-- Root Context: defines shared resources visible to all other web components -->
    <!-- Oracle 연동 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 드라이버 클래스 이름이 변경됨 -->
        <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
        <!-- 연결문자열에 log4jdbc가 추가됨 -->
        <property name="url"
            value="jdbc:log4jdbc:oracle:thin:@localhost:1521:xe" />
        <property name="username" value="spring" />
        <property name="password" value="1234" />
    </bean>
    
    <!-- MySQL 연동  -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/스키마이름?useSSL=false&amp;serverTimezone=UTC">
        </property>
        <property name="username" value="MySQL 계정"></property>
        <property name="password" value="비밀번호"></property>
    </bean>
    
    <!-- 아파치 DBCP 설정 -->
    <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" 
        /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> 
        <property name="username" value="java" /> <property name="password" value="java1234" 
        /> </bean> -->
    <!-- SqlSessionFactory 객체 주입 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- mybatis 설정파일의 경로 -->
        <property name="configLocation" 
            value="classpath:/mybatis-config.xml"></property>
            <!-- mybatis mapper 파일의 경로 -->
        <property name="mapperLocations" 
            value="classpath:mappers/**/*Mapper.xml"></property>
    </bean>
    <!-- SqlSession 객체 주입 -->
    <bean id="sqlSession" 
    class="org.mybatis.spring.SqlSessionTemplate"
        destroy-method="clearCache">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
 
</beans>
cs


servlet-context.xml 파일 : 서블릿에 관련된 설정. 뷰(jsp 페이지)의 접두어,접미어 설정,

파일 업로드 관련 설정, css/js/img 등 리소스 파일들에 대한 설정 정보들이 포함.

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
29
30
31
32
33
34
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <!-- 어노테이션 사용 (HandlerMapping과 HandlerAdapter를 사용할 수 있게 해준다.) -->
    <annotation-driven />
 
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <!-- js, css, img 등 파일을 넣고 필요할 때 꺼내 쓸수 있게 매핑 해둔다. (안하면 DispatcherServlet이 걸림돌이 된다) -->
    <resources mapping="/resources/**" location="/resources/" />
    
    <!-- location="리소스의 실제 경로" mapping="리소스의 가상 url" 가상 url로 매핑 -->    
    <resources location="/WEB-INF/views/include/" mapping="/include/**" />
 
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <!-- 뷰 리졸버 : 뷰(jsp 페이지)의 접두어,접미어 설정 -->
    <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>
 
    <!-- 스프링에서 관리하는 bean의 기본 디렉토리 -->
    <!-- 컴포넌트 스캔 : 해당 패키지 아래에 있는 컴포넌트들을 검색해 빈 객채로 등록 해준다 -->
    <context:component-scan base-package="com.example.spring01" />
                
</beans:beans>
cs



스프링 WEB MVC 설정 프로세스

1. web.xml에 DispatcherServlet 설정
2. web.xml에 캐릭터 인코딩 처리 위한 필터 설정
3. 스프링 MVC 설정
  a. HandlerMapping, HandlerAdapter 설정 (<annotation-driven />)
  b. ViewResolver 설정