새소식

반응형
Java/Spring

Dynamic Web Project 만들기 2 - 프로젝트 기본 설정(1)

  • -
반응형

1. 프로젝트 라이브러리 및 의존성 관리를 위한 maven 설정

1-1. maven 설정 파일인 pom.xml 추가

1-2. Finish 버튼 클릭!


1-3. pom.xml 파일 생성 확인

 

 

2. Spring mvc 설정을 위한 dependency 추가

2-1. mvc 관련 dependency 추가

<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>

빨간 박스안의 내용만 추가했다. 나머지 내용은 pom.xml 생성시 자동 생성된다.

 

2-2. 라이브러리 및 의존성 추가를 위한 maven update

 

2-3. 메이븐 업데이트 프로젝트 확인 후 OK

 

2-4. 추가된 Maven 라이브러리 확인

아래와 같이 Spring mvc에 필요한 jar 파일이 추가됐다.

 

3. web.xml 설정

web.xml파일은 Deployment Descriptor 로써 해당 프로젝트인 어플리케이션의 환경설정 파일이다.

WAS 구동 시 /WEB-INF에 존재하는 web.xml 파일을 읽어 웹 어플리케이션의 설정을 구성한다.

프로젝트 초기 생성 시 web.xml 파일은 welcome 태그 설정밖에 없다.

Spring mvc의 web.xml 파일에서 설정하는건 보통 contextLoaderListener, RootApplicationContext 공간인

root context 설정, view와 관련된 설정을 하는 DispatcherServlet 설정이다.

 

* contextLoaderListener

웹 어플리케이션의 시작과 종료를 담당하는 이벤트 핸들러이다. 

contextLoaderListener는 web.xml 파일을 읽어 각종 Context를 생성하는 클래스이다.

Spring Context는 아래글을 확인해주세요

https://okimaru.tistory.com/233

 

Spring Context

Spring Context Spring Context란 Bean의 확장 버전으로 Bean들을 포함하여 여러 기능을 가진 공간이라고 생각하면 편하다. 보통은 Spring 에서 web.xml에 컨텍스트 설정을 한다. ContextLoaderListener (context..

okimaru.tistory.com

 

* DispatcherServlet

DispatcherServlet은 클라이언트로부터 들어오는 모든 요청을 가장 먼저 받아 적합한 Controller에게 위임해주는

Front Conroller 라고 정의합니다.

https://okimaru.tistory.com/56

 

Dispatcher Servlet(Front Controller), ViewResolver

기존 Servlet 방식 사용자의 요청을 servlet에게 전달하기 위해선 web.xml의 태그를 통해 servlet을 등록한다. testServlet servlet class ​ testServlet /hello ​ 등록된 servlet에 해당하는 url pattern을 체..

okimaru.tistory.com

 

3-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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>Tiles</display-name>

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

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
	<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/spring/appServlet/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<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>
</web-app>

 

ContextLoaderListener 설정

: 프로젝트의 모든 빈 설정을 공유하기 위한 공간인 RootApplicationContext 생성

ContextLoaderListener는 context-param 태그의 root-context.xml 파일을 읽어 RootApplicationContext 공간을 생성한다.

또한 WebApplicationContext 공간을 생성하기 위한 DispatcherServlet도 설정했다.

 

3-2. root-context.xml, dispatcher-servlet.xml 파일 생성

 

4. context 설정

4-1. 설정된 Bean을 어플리케이션 전체에서 공유하는 공통 Bean 설정 파일인

root-context.xml(RootApplicationContext) 기본 설정

- /WEB-INF/spring/root-context.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.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.2.xsd">
     
</beans>

 

4-2. View와 관련된 설정을 하는 dispatcher-servlet.xml(WebApplicationContext) 파일 설정

- /WEB-INF/spring/appServlet/dispatcher-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.2.xsd">
     
</beans>

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.