본문 바로가기
Develop/Spring

[1.개발환경의 설정]2. 스프링 MVC 프로젝트 템플릿의 구조

by jaekk 2018. 8. 10.


Navigator로 본 화면 


Maven의 구조는 크게 

-src: 실제 구현하는 소스

test폴더는 main에 있는 소스로 테스트한다.

resource: 자바가 아닌 파일들 다 넣기(ex: 설정파일)

webapp > resouce를 src>resource에 다 넣기도 한다.

-target: 메이븐을 통해 컴파일 되어 떨어지는 소스



web-inf>classes: 컴파일되면 이 폴더로 다 들어옴




ex00 아래의 디렉토리들을 '프로젝트 레벨'이라고 한다


pom.xml

메이븐 파일

groupId: 서버

artifactId: 업무 구분

*artifactId 가 context 경로가 된다


실행을 하고 나면 제일 먼저 web.xml 확인


 

root-context.xml을 쓰기 위하여는 listener를 사용하여야 한다.


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
32
33
34
<?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>
 
cs


web.xml - DispatcherServlet : 웹 담당

1
2
3
4
5
6
7
8
9
10
    <!-- 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>
cs



/WEB-INF/spring/appServlet/servlet-context.xml에 Controller 정보 있음

1
2
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
cs


JSP의 서블릿

1
2
3
4
5
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
cs


매핑된 url정보를 보고

ex) <url-pattern>/</url-pattern>

DispatcherServlet이랑 매핑되어 DispatcherServlet 실행




servlet-context.xml

resource는 /resources/와 매핑 되어 있음

위치: src > main > webapp > resource


※ 서버 백단 src>main>java or src>main>resource

   프론트엔드단 src>main>webapp  

 

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
<?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:bean>
    
    <context:component-scan base-package="org.zerock.web" />
    
    
    
</beans:beans>
 
cs


빈 자동 등록

1
2
<context:component-scan base-package="org.zerock.web" />
 
cs

org.zerock.web.HomeController.java


return "home"; 코드가

beans:property 속성과 맞물려 동작함

=> /WEB-INF/views/home.jsp 


1
2
3
4
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
cs

 

'Develop > Spring' 카테고리의 다른 글

[3.MySQL의 설정과 스프링테스트]1.MySQL Workbench사용  (0) 2018.08.10
[2.스프링소개]1.스프링 소개  (0) 2018.08.10
[1.개발환경의 설정]1.spring project 생성  (0) 2018.08.10
스프링  (0) 2018.07.24
스프링 정리2  (0) 2018.06.24

댓글