1. 이클립스에서 New -> Dynamic Web Project 선택

 

 

 

 

 

2. Dynamic Web Project 설정

 

Project name : 프로젝트명

Target runtime : Apache Tomcat v7.0

Dynamic web module version : 3.0

 

설정 후 Next

 

 

 

 

Configure project for building a Java application 화면에서 Source folders on build path의 src 제거 후 Next ~~

( 추후 Maven 설정으로 재설정 )

 

 

 

 

Configure web mobule settings에서 Content directory 의 내용을 webapp로 변경 후 Finish~

 

 

 

 

 

 

3. maven project 로 변환

 

프로젝트 선택 후 마우스 우클릭 > Configure > Convert to Maven Project 선택

 

 

 

Create new POM 에서 Finish~~

 

 

 

4. source 구조 생성 및 Java Build Pah 추가

Maven 관례에 따라 디렉토리 구조를 만듭니다.

src/main/java
src/main/resources
src/test/java
src/test/resources

 

디렉토리 생성 후 프로젝트 선택 > 우클릭 > Propertice > Java Build Path > Source를 선택

 

Add Folder 클릭하여 아래 폴더들을 선택합니다.

 

src/main/java
src/main/resources
src/test/java
src/test/resources

 

이미 src/main/java 와 src/test/java 선택되어 있다면 src/main/resources 와 src/test/resources 선택 후 OK 버튼 클릭

 

 

 

 

5. web.xml 파일 생성

 

webapp > WEB-INF 에 web.xml 파일을 생성 후 아래 내용을 추가한다. 

 

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 
 
 
</web-app>

 

 

6. UTF-8 encoding 변경

다국어를 지원하는 웹어플개발에서는 이클립스에서 인코딩을 UTF-8로 설정해야 추후 발생할 문자셋관련 에러를 방지할 수 있습니다.

 

상단 메뉴 중 Window > Properties 에서 General > Workspace 선택.. Text file encoding을 UTF-8로 변경하고 OK 클릭~~

 

 

 

이클립에서 설정했어도 가끔 빌드시에 에러가 나는경우가 있으므로 pom.xml 에 다음 항목을 추가한다.

 

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>MavenWebProject</groupId>
 <artifactId>MavenWebProject</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
     <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>webapp</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

 

저장 후 메이븐 > 업데이트 프로젝트 ~~

 

7. pom.xml 에 서블릿, JSP, JSTL, 스프링 MVC dependency 추가

 

<dependencies>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.2</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.0.4.RELEASE</version>
  </dependency>
</dependencies>

 

8. web.xml 파일 수정

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

 <!-- DispathcherSerlvet 설정 -->
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet

  </servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/applicationContext.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.do</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>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

 


9. applicationContext.xml 파일 생성

WEB-INF 폴더 밑에 applicationContext.xml 파일 생성 후 아래 내용을 붙여넣는다.

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans    
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

 

 <mvc:annotation-driven/>

 

 <context:component-scan base-package="com.test.web" />

 

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/view/" />
  <property name="suffix" value=".jsp" />
 </bean>

 

</beans>

 

 

10. jsp 폴더 생성

applicationContext.xml 내 정의된 내용 중 viewResolver의 경로를 /WEB-INF/view 폴더로 지정하였다.

추후 컨트롤러에서 jsp 사용시 /WEB-INF/view 안에 jsp를 찾게 된다.

현재 WEB-INF 폴더안에 view 폴더가 없으므로 새로 생성한다.

 

11. 기본 패키지 생성

기본 패키지는 com.test.web으로 시작할 것이다.

만약 패키지가 변경된다면 applicationContext.xml의 <context:component-scan base-package="com.test.web" /> 부분도 같이 변경해야한다.

 

12. 컨트롤러 생성

com.test.web 패키지 밑에 index.controller 패키지 추가 후 IndexController.java 파일을 만들고 아래 내용을 붙여넣는다.

 

package com.test.web.index.controller;

 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller
public class IndexController {
 
 @RequestMapping("index")
 public String index(){
  return "index";
 }
 
}

 

13. JSP 생성

/WEB-INF/view 폴더 안에 index.jsp 파일을 생성 후 아래 내용을 붙여넣는다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
 </head>
 <body>
  Hello, world!
 </body>
</html>

 

14. Context root 수정

이클립스에서 프로젝트 생성시 기본 Context는 프로젝트명으로 설정된다.

이 부분을 /로 변경한다.

 

프로젝트 선택 후 우클릭 > Properties > Web Project Settings 에서 Context root 값을 /로 수정 후 OK ~~

 

15. tomcat 서버 셋팅

 

상단 메뉴 > Window > Show View 에서 Server 창을 활성화 시킨다.

 

 

Server 창 빈공간에서 마우스 우클릭 > New > Server

 

 

 

New Server 창에서 필요한 정보를 선택 또는 입력한다.

 

Server : Tomcat v7.0 Server

Server's host name : localhost ( 기본값 )

Server name : MavenWebProject ( 프로젝트명 )

 

 

 

만들어진 서버명을 더블클릭하여 세부 설정을 한다.

 

우선 간단하게 포트만 변경한다.

 

HTTP/1.1의 기본 포트가 8080 에서 80 으로 수정

 

 

 

다음으로 만들어놓은 프로젝트를 추가한다.

 

서버 설정 하단에 Modules 탭을 클릭한다.

 

 

 

우측에 Add Web Module... 클릭..

 

 

 

Add Web Module 창에서 프로젝트명 클릭 후 OK ~~

 

설정 파일을 저장한다.

 

16. tomcat 기동

 

서버창에서 프로젝트 클릭 후 플레이 아이콘 클릭

 

 

 

서버가 실행되었을 경우 Colsole창이 활성화되면서 로그를 확인 할 수 있다.

 

 

 

톰캣 서버가 정상실행되었을 경우

서버창에서는 서버상태가 Started로 변경되고 Colsole창에선 에러없이 아래와 같은 메시지가 뜬다.

 

정보: Server startup in 2285 ms

 

만약, Colsole 창에서 Exception이 발생한다면 이때까지 진행했던 내용을 재검토 하기 바란다.

 

17. Web Browser 에서 확인

작업을 하면서 테스트를 조금이나마 편리하게 하기 위해 port와 context정보를 최대한으로 줄였다.

 

http://localhost/index.do

 

 

 

위와 같이 Hello, world!가 뜨면 기본 설정이 끝났다.

 

끝 ~~~

 

 
블로그 이미지

애니스

카테고리

분류 전체보기 (10)
프로그래밍 (10)
애니스전용 (0)