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

2022. 2. 6. 14:00·Spring/Spring

스프링 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>
'Spring/Spring' 카테고리의 다른 글
  • [Spring] RestClient URI Encoding 문제 (feat. 퍼센트 인코딩)
  • [Spring] Controller와 어노테이션
  • [Spring] 스프링 프레임워크 의존관계
  • [Spring] 스프링 프레임워크란?
gakko
gakko
좌충우돌 개발기
  • gakko
    MYVELOP 마이벨롭
    gakko
  • 전체
    오늘
    어제
    • 분류 전체보기 (205) N
      • Spring (23)
        • Spring (10)
        • Spring Boot (7)
        • Spring Security (1)
        • Hibernate (4)
      • Test (3)
      • 끄적끄적 (6)
      • 활동 (35)
        • 부스트캠프 (23)
        • 동아리 (3)
        • 컨퍼런스 (3)
        • 글또 (5)
        • 오픈소스 컨트리뷰션 (1)
      • 디자인패턴 (0)
      • Git & GitHub (22)
        • Git (13)
        • Github Actions (1)
        • 오류해결 (5)
        • 기타(마크다운 등) (3)
      • 리눅스 (6)
        • 기초 (6)
        • 리눅스 서버 구축하기 (0)
      • Infra (2)
        • Docker (1)
        • Elastic Search (0)
        • Jenkins (1)
        • AWS (1)
      • MySQL (7)
        • 기초 (6)
        • Real MySQL (1)
      • 후기 (3)
        • Udemy 리뷰 (3)
      • CS (26)
        • 웹 기본지식 (0)
        • 자료구조 (13)
        • 운영체제 OS (12)
        • 데이터베이스 (1)
        • 시스템 프로그래밍 (0)
        • 기타 (0)
      • Tools (1)
        • 이클립스 (1)
        • IntelliJ (0)
      • 프로젝트 (2) N
        • 모여모여(부스트캠프) (1)
      • JAVA (32)
        • Maven (6)
        • 오류해결 (11)
        • 자바 클래스&메소드 (1)
        • JSP & Servlet (12)
      • Javascript (5)
        • 기초 (3)
        • React (2)
      • Python (28)
        • 파이썬 함수 (9)
        • 알고리즘 문제풀이 (16)
        • 데이터 사이언스 (2)
        • 웹 크롤링 (1)
      • 단순정보전달글 저장소 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 우진님
  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    자바스크립트
    java
    스프링
    부스트캠프
    웹개발
    부스트캠프 멤버십
    자바
    os
    jsp
    스프링부트
    운영체제
    부스트캠프 7기
    Git
    GitHub
    Spring
    오류해결
    파이썬
    MySQL
    Python
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
gakko
[Spring] 스프링 MVC 사용을 위한 의존관계 추가
상단으로

티스토리툴바