처음 스프링 프로젝트가 생성되면 아래와 그림과 같은 구조를 확인 하실 수 있습니다.
간단하게 구성을 설명 하겠습니다.
src/main/java 는 프로젝트에서 개발되는 모든 Java코의 경로가 됩니다.
개발자나 프로젝트의 기호에 따라 sql mapper, tiles 등 xml 의 경로가 포함되기도 합니다.
src/main/recoursec 는 서버가 작동될때 사용되는 파일들의 경로입니다.
DB 연동을 위한 sql mapper의 xml 파일, titles의 template.xml 파일, 다국어 지원을 위한 properties.xml 등이 포함됩니다.
src/test/java 와 src/test/resources 는 말 그대로 테스트용 경로입니다. 위 두 경로와 목적만 다릅니다.
JRE System Library 세팅된 자바 버전을 나타냅니다.
Maven Dependencies 는 pom.xml 에 등록된 라이브러리들이 표시 되어 있습니다.
src->webapp->resources 는 이미지, 자바스크립트 등 페이지를 구성하는 resources들의 경로입니다.
src->webapp->WEN-INF->spring 스프링을 구성하는 설정 파일들의 경로입니다.
src->webapp->WEB-INF->views 실제 페이지를 구성하는 jsp, html 등의 파일들의 경로입니다.
아래부터 home.jsp 의 작동 원리를 설명 하겠습니다.
기존 Dynamic Web 프로젝트는 jsp 등 웹 파일에서 서버를 실행 하면 바로 해당 페이지에 접근을 할 수 있었습니다.
하지만 스프링에서는 보안을 위해 웹 파일들을 WEB-INF 안에 저장 하고 있습니다.
이유는 WEB-INF 는 직접 접근을 할 수 없기 때문입니다. 이러한 이유로 home.jsp 를 바로 실행 시키면
아래처럼 404 에러가 발생합니다. 그렇기 때문에 우리는 컨트롤러를 통해 페이지를 호출 해야 합니다.
그림(스프링 작동 순서) 에서 부터 순서대로 설명 하겠습니다.
- 그림(스프링 작동 순서)
1. web.xml 은 프로젝트의 환경 설정을 해주는 파일입니다. 프로젝트가 실행되면 이 파일이 가장 먼저 실행 되고,
DispatcherServlet이 해당 프로젝트로 들어오는 요청들을 낚아채고 핸들링을 해줍니다.
이 부분은 낚아챈 요청들을 servlet-context.xml 로 보내주는 부분입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!--?xml version="1.0" encoding="UTF-8"?--> < web-app version = "2.5" 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_2_5.xsd" > <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/spring/root-context.xml</ param-value > </ context-param > <!-- Creates the Spring Container shared by all Servlets and Filters --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- Processes application requests --> < 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/servlet-context.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 > </ web-app > |
2. servlet-context 는 낚아챈 요청을 받아 <annotation-drive/> 란 태그를 사용하여 컨트롤러에서 어노테이션을 사용할 수 있게 해줍니다.
<context:component-scan base="com.프로젝트.패키지명"/> 태그는 어노테이션을 사용할 영역(패키지)를 설정 할 수 있게 해줍니다.
낚아챈 요청은 com.포르젝트.패키지명에 요청된 url에 맞게 맵핑이 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!--?xml version="1.0" encoding="UTF-8"?--> < beans:beans xmlns = "http://www.springframework.org/schema/mvc" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:beans = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" 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.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> < annotation-driven > <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> < resources mapping = "/resources/**" location = "/resources/" > <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> < beans:bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < beans:property name = "prefix" value = "/WEB-INF/views/" > < beans:property name = "suffix" value = ".jsp" > </ beans:property ></ beans:property ></ beans:bean > < context:component-scan base-package = "com.black.user.controller" > </ context:component-scan ></ resources ></ annotation-driven ></ beans:beans > |
3. @RequestMapping(value = "/", method=RequestMethod.GET) 에 있는 value 값을 참조하여 요청된 url과 맵핑을 합니다.
return 되는 값은 "home" 이지만, 과정 4에서 페이지 경로를 prefix(/WEB-INF/views/) , suffix(.jsp) 가 자동으로 완성해주기 때문에,
최종적으로 /WEB-INF/view/return값.jsp 가 호출이 됩니다.
localhost:8080 을 실행 해보시면 아래와 같은 결과가 나오게 됩니다.
@RequestMapping 에서 value값을 "Index" 라고 설정을 하게 되면, localhost:8080/Index 라는 url로 접근 하면 됩니다.
'Programming > Spring' 카테고리의 다른 글
[스프링부트/그레이들] 01_스프링부트 그레이들 환경 프로젝트 생성 (0) | 2019.03.25 |
---|---|
[스프링] 03_스프링 Mybatis, Mybatis-spring 설정 (0) | 2019.03.15 |
[스프링] 01_스프링 프로젝트 생성 (0) | 2019.03.12 |
[스프링] 톰캣 서버 설정 (0) | 2019.03.12 |
[스프링/메이븐] 메이븐 로컬 경로 설정 maven local repository path change (0) | 2019.03.11 |