본문 바로가기

STUDY/Spring

Spring | Dynamic Web Project로 스프링 프로젝트 생성하기

※ Spring Legacy Project로 생성하는 법

1. Dynamic Web Project 생성

Dynamic Web Project?
이클립스(Eclipse)에서 생성하는 Servlet/JSP기반 동적 웹 애플리케이션 프로젝트

 

File - New - Dynamic Web Project

 

 

Project name입력 후 특별히 설정할 것이 없으므로 Finish

톰캣 서버 일치하는지 확인하기

 

 

생성 완료

 

 

 

2. pom.xml생성

 

해당 프로젝트 우클릭 - Configure - Convert to Mave Project

 

 

생성완료

 

 

 

3. pom.xml에 Spring Web MVC 라이브러리 추가하기

 

mvnrepository접속 후 Spring Web MVC검색, 버전 선택 후 코드 복사

 

Maven Repository: Search/Browse/Explore

Java Lint Assert Samples Last Release on Jun 20, 2020

mvnrepository.com

 

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

 

 

dependency 추가

 

 

 

4. web.xml생성

프로젝트 우클릭 - Java EE Tools - Generate Deployment Descriptor Stub

 

 

WEB-INF폴더 하위에 web.xml파일 생성됨

 

 

기본적으로 웰컴파일 리스트만 설정되어있음

 

 

 

5. dispatcherServlet.xml 작성하기

 

WEB-INF하위에 spring폴더 생성 후 우클릭 - New - Other

 

 

Spring Bean Configuration File선택 - Next

 

 

파일명 입력 후 finish

 

 

생성된 모습

 

 

소스코드창 하단에 Namespaces탭 클릭 - beans / context / mvc 체크

 

 

다시 Source 탭을 클릭하면 변경된 것을 확인할 수 있음

 

 

스프링 어노테이션 설정, views(jsp파일들을 모을 폴더)설정, 자바 패키지 등록

 

 

class 오타 주의할 것!

 

<!-- 어노테이션 사용 활성화 -->
<context:annotation-config />

<!-- view 폴더 위치 및 확장자 설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
</bean>

<!-- 사용할 자바 패키지명 입력 -->
<context:component-scan base-package="spring.com.sample" />

 

 

 

6. web.xml 작성

 

위에서 작성한 dispatcherServlet.xml 매핑

DispatcherServlet은 자바 Servlet 을 확장한 클래스로 특정 URL 패턴을 DispatcherServlet으로 맵핑하여 스프링 MVC를 사용할 수 있도록 해줌

 

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/dispatcherServlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 

 

리스너 설정 

웹 애플리케이션이 서블릿 컨테이너에 로딩될 때 실행되는 리스너. 웹 애플리케이션이 로딩될 때 Web ApplicationContext를 만듦.
WebApplicationContext 는 contextConfigLocation에 설정한 빈 설정 파일을 사용해서 웹 애플리케이션 에서 사용할 객체를 관리해주는 역할을 함.

 

<!-- Listener -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 

인코딩 UTF-8설정

 

<!-- 한글 설정 -->
<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.xml

<?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" version="3.1">
  <display-name>SpringSample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map all requests to the DispatcherServlet for handling -->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
  </context-param>

  <!-- 서블릿과 필터에 공유할 수 있는 리스너 설정 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 한글 설정 -->
  <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>

 

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>SpringSample</groupId>
  <artifactId>SpringSample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>5.2.5.RELEASE</version>
	</dependency>
  </dependencies>
</project>

 

dispatcherServlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">
	
	<!-- 어노테이션 사용 활성화 -->
	<context:annotation-config />
	
	<!-- view 폴더 위치 및 확장자 설정 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 사용할 자바 패키지명 입력 -->
	<context:component-scan base-package="spring.com.sample" />

</beans>