티스토리 뷰

[ Spring Legacy Project 로 생성방식 바로가기 ]

http://vip00112.tistory.com/27

 

- Spring Framework로 개발을 하기 위한 project 생성 방법

 

1. 좌측 Project Explorer - 마우스 우클릭 - New - Project 클릭

 

2. Web - Dynamic Web Project 선택 후 Next 버튼 클릭

 

3. Project Name 입력 후 Next 버튼 클릭

 

4. Next 버튼 클릭

 

5. Context root를 " / " 로 변경, Generate web.xml deployment descriptor 체크 후 Finish 버튼 클릭

 

6. Project를 Spring 환경으로 변경

1) Project 우클릭 - Spring Tools - Add Spring Project Nature 클릭

 

7. 정상적으로 적용이 되면 Project에 S모양 아이콘이 생긴다.

 

8. WebContent/lib 폴더에 Library 파일 추가 : Spring 필수, 파일 업로드, taglibs, MyBatis를 이용하기 위한 필수 목록

- Maven Project 일시 이 과정 대신 pox.xml 설정

 

9. Spring 설정 파일 생성

1) WebContent - WEB-INF 우클릭 - New - Other 클릭

2) Spring 폴더 - Spring Bean Conriguration File 선택 후 Next 버튼 클릭

3) File name 작성(applicationContext.xml) 후 Next 버튼 클릭

4) beans, jee, p 체크 후 Finish 버튼 클릭

 

10. DB 연결을 위한 META-INF/context.xml 파일 생성 후 설정

- 본문 에서는 oracle 기준이며, DB마다 설정 내용이 다를 수 있음

 

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource  name="oraclexe" auth="Container" type="javax.sql.DataSource"
	driverClassName="oracle.jdbc.OracleDriver"
	url="jdbc:oracle:thin:@localhost:1521:xe"
	username="계정" password="비밀번호"
	maxIdle="10" maxActive="20" maxWait="-1"
	/>
</Context>

 

11. MyBatis 사용을 위한 WebContent/WEB-INF/mybatis-config.xml 파일 생성 후 설정

- 본문 에서는 oracle 기준이며, DB마다 설정 내용이 다를 수 있음

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org/DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- NULL 처리 -->
    <settings>
        <setting name="jdbcTypeForNull" value="NULL" />
    </settings>

    <!-- Aliases -->
    <typeAliases>
        <typeAlias alias="User" type="vo.User" />
    </typeAliases>

    <!-- Mappers -->
    <mappers>
        <mapper resource="mapper/users.xml" />
    </mappers>
</configuration>

 

12. WebContent/WEB-INF/applicationContext.xml 에서 의존성 주입(Dependency Injection)

<?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:jee="http://www.springframework.org/schema/jee" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd">

    <!-- DataSource -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/oraclexe" />

    <!-- SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- SqlSessionTemplate -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <!-- Mulitipart Resolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="maxUploadSize" value="104857600" />
    </bean>

    <!-- Util -->
    <bean id="paginateUtil" class="util.PaginateUtil" />

    <!-- DAO -->
    <bean id="usersDAO" class="dao.UsersDAOImpl">
        <property name="session" ref="sqlSession" />
    </bean>

    <!-- Service -->
    <bean id="usersService" class="service.UsersServiceImpl">
        <property name="usersDAO" ref="usersDAO" />
    </bean>
</beans>

 

13. WebContent/WEB-INF/web.xml에 WelcomePage,ErrorPage 등의 웹 기본 설정과 Listener, DispatcherServlet 등의 Spring 연결 설정

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <!-- Web Application의 이름 및 설명 -->
    <display-name>SimpleBoard</display-name>
    <description>SimpleBoard</description>

    <!-- Tomcat 서버 구동시 META-INF의 context.xml 읽기 설정 -->
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>oraclexe</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <!-- welcom page -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- error page -->
    <error-page>
        <error-code>404</error-code><!-- not found -->
        <location>/WEB-INF/view/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/view/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>405</error-code><!-- Request method 'GET' not supported -->
        <location>/WEB-INF/view/error.jsp</location>
    </error-page>

    <!-- POST방식의 한글처리 Filter 설정 -->
    <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>

    <!-- POST방식일때 _method(PUT/DELETE)요청 Filter 설정 -->
    <filter>
        <filter-name>method</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>method</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- applicationContext.xml을 읽어오는 리스너 설정 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Front Controller인 DispatcherServlet 등록 -->
    <servlet>
        <servlet-name>simpleBoard</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 서버가 켜질때 servlet 바로 생성 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- DispatcherServlet url mapping -->
    <servlet-mapping>
        <servlet-name>simpleBoard</servlet-name>
        <url-pattern>/</url-pattern> <!-- REST -->
    </servlet-mapping>
</web-app>

 

14. WebContent/WEB-INF 하위에 web.xml에서 mapping한 DispatcherServlet의 name과 동일한 servlet.xml 파일 생성(simpleBoard-servlet.xml) 후 의존성 주입(Dependency Injection)

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 기본 설정 -->
    <mvc:annotation-driven ignore-default-model-on-redirect="true" />

    <!-- Internal Resouce View 접두/접미어 설정 -->
    <mvc:view-resolvers>
        <mvc:jsp prefix="/WEB-INF/view/" suffix=".jsp" />
    </mvc:view-resolvers>

    <!-- Model전달이 필요없는 Controller -->
    <mvc:view-controller path="/intro" />

    <!-- 정적인 리소스 명시 -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/fonts/" mapping="/fonts/**" />

    <!-- interceptor 설정 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!-- interceptor 적용할 접근 경로 -->
            <mvc:mapping path="/**" />

            <!-- interceptor 제외할 접근 경로 -->
            <mvc:exclude-mapping path="/intro" />
            <mvc:exclude-mapping path="/session" />
            <mvc:exclude-mapping path="/join/**" />

            <!-- 정적인 리소스는 interceptor 제외 -->
            <mvc:exclude-mapping path="/css/**" />
            <mvc:exclude-mapping path="/img/**" />
            <mvc:exclude-mapping path="/js/**" />
            <mvc:exclude-mapping path="/fonts/**" />
            <bean class="interceptor.LoginCheckInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- Spring Web MVC에 대한 설정 (Controller) -->
    <bean class="controller.UserController">
        <property name="userService" ref="userService" />
    </bean>
</beans>

 

댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday