티스토리 뷰

[ Dynamic Web Project 로 생성방식 바로가기 ]

http://vip00112.tistory.com/17

 

 

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

 

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

 

2. Spring - Spring Legacy Project 선택 후 Next 버튼 클릭

 

3. 상단 Project Name 입력

 

4. Templates에서 Spring MVC Project 선택 후 Next 버튼 클릭

 

5. package명 설정 후 Finish 버튼 클릭

1) 여기까지 진행시 Maven을 이용한 라이브러리를 다운로드가 자동으로 진행 된다.

2) 프로젝트 - Java Resources - Libraries - Maven Dependencies 에서 필요한 라이브러리가 잘 들어있는지 확인 가능

 

6. pom.xml 에서 Java와 Spring 버전을 설정

<properties>
	<java-version>1.8</java-version>
	<org.springframework-version>4.2.5.RELEASE</org.springframework-version>
	<org.aspectj-version>1.8.9</org.aspectj-version>
	<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>

 

7. 프로젝트 마우스 우클릭 - Build Path - Configure Build Path - Libraries 변경

1) JRE System Library 클릭 후 Edit 버튼 클릭 - Workspace default JRE 선택

2) Add Library 버튼 클릭 - Server Runtime - Apatch Tomcat v8.0 선택

3) Add Library 버튼 클릭 - Web App Libraries - Project 콤보박스에서 현재 프로젝트 선택

 

8. 프로젝트 마우스 우클릭 - Properties - Java Compiler 변경

1) Compiler compliance level을 1.8로 변경

 

9. 프로젝트 마우스 우클릭 - Properties - Project Facets 변경

1) Dynamic Web Module 버전을 3.1로 변경

2) Java 버전을 1.8로 변경

 

10. 프로젝트 마우스 우클릭 - Properties - Web Project Setting 변경

1) Context root를 '/'로 변경

 

11. webapp/WEB-INF/classes/META-INF 폴더가 없거나 비어있을 경우 Tomcat 구동시 에러가 발생한다.

1) Tomcat 8.0.32 ~ 8.0.35 버전 사이에서 발생하는 버그 이다.

 

12. webapp/WEB-INF/web.xml DTD를 3.1 버전에 맞게 변경

<web-app version="3.1" 
	 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">

</web-app>

 

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

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

- webapp/META-INF 폴더가 없을경우 생성

- 추후 호스팅 서버에서 실제 서비스를 할 경우 tomcat/conf/server.xml의 Context태그 안에 해당 내용을 추가 해야함

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource
	name="jdbc/mysql"
	auth="Container"
	type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	username="유저명"
	password="비밀번호"
	url="jdbc:mysql://localhost/database이름?useUnicode=true&characterEncoding=utf8"
	maxIdle="10" maxActive="20" maxWait="-1" />
</Context>

 

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

- 본문 에서는 MySQL 기준이며, 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>

 

15. webapp/WEB-INF/config/root-context.xml 에서 의존성 주입(Dependency Injection)

1) root-context.xml DTD 추가 (또는 Namespace 에서 체크박스 체크)

- aop, beans, jee, p, tx

2) xml 코드 추가

3) Util, DAO, Service 등 프로젝트 설정에 따라 class와 내용이 다를 수 있음

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	<!-- DataSource -->
	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/mysql" />
	<!-- SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="/WEB-INF/config/mybatis-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- SqlSessionTemplate -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>
	<!-- 트랜잭션 매니저 설정 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="*TX" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<!-- service패키지와 하위 패키지안에 접근제한자가 public 이고 인자가 0개 이상인 모든 메서드 -->
		<aop:pointcut id="txPoint" expression="execution(public * com.gt.board.service.*.*(..))" />
		<aop:advisor pointcut-ref="txPoint" advice-ref="txAdvice" />
	</aop:config>
	<!-- // 트랜잭션 매니저 설정 끝 -->
	<!-- 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="com.gt.board.util.PaginateUtil" />
	<bean id="fileRenameUtil" class="com.gt.board.util.FileRenameUtil" />
	<bean id="cookieUtil" class="com.gt.board.util.CookieUtil" />
	<bean id="dateUtil" class="com.gt.board.util.DateUtil" />
	<bean id="imageFileUtil" class="com.gt.board.util.ImageFileUtil" />
	<!-- DAO -->
	<bean id="usersDAO" class="com.gt.board.dao.UsersDAOImpl">
		<property name="session" ref="sqlSession" />
	</bean>
	<!-- Service -->
	<bean id="userService" class="com.gt.board.service.UserServiceImpl">
		<property name="usersDAO" ref="usersDAO" />
		<property name="commentsDAO" ref="commentsDAO" />
		<property name="driversDAO" ref="driversDAO" />
		<property name="inquiresDAO" ref="inquiresDAO" />
		<property name="workingDAO" ref="workingDAO" />
	</bean>
	<!-- 이메일 관련 -->
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.gmail.com" />
		<property name="port" value="587" />
		<property name="username" value="아이디@gmail.com" />
		<property name="password" value="비밀번호" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
				<prop key="mail.smtp.starttls.enable">true</prop>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
	</bean>
</beans>

 

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
	<display-name>GTBoard</display-name>
	<description>GT's Board Template</description>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<error-page>
		<error-code>404</error-code>
		<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>
		<location>/WEB-INF/view/error.jsp</location>
	</error-page>
	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>
	<mime-mapping>
		<extension>zip</extension>
		<mime-type>application/zip</mime-type>
	</mime-mapping>
	<mime-mapping>
		<extension>hwp</extension>
		<mime-type>application/unknown</mime-type>
	</mime-mapping>

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/root-context.xml</param-value>
	</context-param>

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<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>
	<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>
	<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/config/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>captcha</servlet-name>
		<servlet-class>servlet.CaptchaServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>captcha</servlet-name>
		<url-pattern>/captcha</url-pattern>
	</servlet-mapping>

</web-app>

 

17. webapp/WEB-INF/config/servlet-context.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="/login" />
	<mvc:view-controller path="/error" />

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

	<!-- interceptor 설정 -->
	<mvc:interceptors>
		<!-- 로그인 확인 -->
		<mvc:interceptor>
			<!-- interceptor 적용할 접근 경로 -->
			<mvc:mapping path="/**/write" />
			<mvc:mapping path="/**/update" />
			<mvc:mapping path="/**/delete" />
			<mvc:mapping path="/working/**" />
			<mvc:mapping path="/inquire/**" />
			<mvc:mapping path="/user/**" />
			<mvc:mapping path="/admin/**" />
			<mvc:mapping path="/imgUpload" />

			<!-- interceptor 제외 -->
			<mvc:exclude-mapping path="/working" />
			<mvc:exclude-mapping path="/working/list" />

			<!-- 정적인 리소스는 interceptor 제외 -->
			<mvc:exclude-mapping path="/resources/**" />
			<mvc:exclude-mapping path="/smarteditor/**" />
			<bean class="com.gt.board.interceptor.LoginCheckInterceptor" />
		</mvc:interceptor>

		<!-- 모든 페이지 -->
		<mvc:interceptor>
			<mvc:mapping path="/**" />

			<!-- 정적인 리소스는 interceptor 제외 -->
			<mvc:exclude-mapping path="/resources/**" />
			<mvc:exclude-mapping path="/smarteditor/**" />
			<bean class="com.gt.board.interceptor.GlobalModelInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

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

</beans>

 

 

 

* webapp/config 폴더를 생성하여 각종 xml 설정 파일들을 한 곳에 몰아 두었다.

- root-context.xml, servlet-context.xml, mtbatis-config.xml 등

 

* STS를 이용한 프로젝트 생서이 pom.xml에 등록된 라이브러리 만으로는 실제 서비스를 하는데 한계가 있다.

  기본적인 기능을 구현하기 위한 라이브러리 추가를 위해 아래 코드를 pom.xml에 추가 한다.

<dependency>
	<groupId>aopalliance</groupId>
	<artifactId>aopalliance</artifactId>
	<version>1.0</version>
</dependency>
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.8.9</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>${org.springframework-version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>${org.springframework-version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-websocket</artifactId>
	<version>${org.springframework-version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>${org.springframework-version}</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.3.0</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
	<version>1.2.4</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.39</version>
</dependency>
<dependency>
	<groupId>taglibs</groupId>
	<artifactId>standard</artifactId>
	<version>1.1.2</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>
<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.3.1</version>
</dependency>
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.7</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.7.0</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.7.0</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.7.0</version>
</dependency>
<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>javax.mail-api</artifactId>
	<version>1.5.6</version>
</dependency>
<dependency> <!-- maven 버그로 인해 다운로드가 안되므로 로컬에 위치함 -->
	<groupId>nl.captcha</groupId>
	<artifactId>simplecaptcha</artifactId>
	<version>1.2.1</version>
	<scope>system</scope>
	<systemPath>${basedir}\src\main\webapp\WEB-INF\lib\simplecaptcha-1.2.1.jar</systemPath>
</dependency>
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday