Spring/Spring

[Spring] 스프링 MVC 사용을 위한 의존관계 추가

gakko 2022. 2. 6. 14:00

스프링 MVC 애플리케이션을 사용하려면 환경설정을 미리 해놓아야한다.

2가지 방법을 나누어 살펴보도록 하자.

첫 번째는 메이븐 프로젝트

두 번째는 일반 동적 웹 프로젝트를 활용할 때의 예시이다.

동적 웹 프로젝트를 활용하면 일일이 라이브러리를 추가해줘야 하기때문에 대부분 메이븐을 사용할 거라고 생각한다.

 

 

 

1. 메이븐 프로젝트


메이븐 프로젝트를 활용할 때의 웹 애플리케이션을 설정하는 순서는 총 3단계로 나누어볼 수 있다.

① pom.xml 에 스프링 MVC의 의존관계를 추가

② DispatcherServlet을 web.xml에 추가

③ servlet.xml 파일로 스프링 애플리케이션 컨텍스트 생성

 

- pom.xml에 추가

<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>5.3.13</version>
</dependency>
  • 버전은 매번 업데이트되므로 의존성 정보는 메이븐 홈페이지에서 검색해 찾아보자

 

 

- web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- DispatcherServlet 설정 -->
  <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/servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <!-- 서블릿 Mapping -->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  • servlet.xml 파일의 경로를 적어준다. (/WEB-INF/ 에 생성)

 

 

- servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 컴포넌트 스캔 -->
	<context:component-scan base-package="com.bigbell.spring" />
	
	<!-- 뷰 리졸버 설정 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
	<!-- MVC 어노테이션 활성화 -->
	<mvc:annotation-driven />

</beans>

 

 

 

 

2. 동적 웹 프로젝트(라이브러리 직접 삽입 방식)


동적 웹 프로젝트를 활용한다면 단계는 총 3가지이다.

① lib파일에 스프링 라이브러리 삽입

② DispatcherServlet을 web.xml에 추가

③ servlet.xml 파일로 스프링 애플리케이션 컨텍스트 생성

1단계를 제외하고선 메이븐 프로젝트와 비슷하다.

 

 

- 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"
	id="WebApp_ID" version="3.1">

	<display-name>spring-mvc-demo</display-name>

	<absolute-ordering />

	<!-- DispatcherServlet 설정 -->
	<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/spring-mvc-demo-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 서블릿 Mapping -->
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

 

 

 

- servlet.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/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
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 컴포넌트 스캔 -->
	<context:component-scan base-package="com.bigbell.springdemo" />


	<!-- 뷰 리졸버 정의 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- MVC 어노테이션 활성화 -->
    <mvc:annotation-driven/>
</beans>