티스토리 뷰

FULL STACK/SPRING &EGOV

Spring MVC 구조

publepuble 2018. 12. 5. 11:57

MVC 구조 



  1. 서블릿에게 URL로 접근하여 해당 정보를 요청한다. 

 클라이언트가 URL을 쏴준다!! 그러면 보내준 URL을 DispatcherServlet이 받는데 이렇 게 받게끔 하는 즉 맵핑을  시켜주는 놈이 web.xml 이다. web.xml을 보면

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

이렇게 되있는데 

<servlet-mapping>  </servlet-mapping> 사이에  <url-pattern> 과 </url-pattern> 사이에 " / " 이것이 클라이언트에서 " / " 이렇게 들어오는 url은

 <servlet-mapping>
           <servlet-name>appServlet</servlet-name>
           <url-pattern>/</url-pattern>
     </servlet-mapping>

이렇게 들어오는 애들은 DispatcherServlet 으로 가라 라고 써있습니다.

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

  1. 핸들러 매핑에 해당요청을 매핑한 컨틀롤러가 있는지 검색요청

매핑한 컨트롤러가 있는지 검색을 요청하는 부분은 web.xml에 또 나와있는데 그부분이 스프링 컨테이너 설정파일 즉, servlet-context.xml 이다.

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

  1. 스프링 컨테이너 설정파일 즉, servlet-context.xml 을 자세히 보자 

<?xml version="1.0" encoding="UTF-8"?>
     <!-- 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="com.javalec.springmvc_ex_project" />
     
</beans:beans>
이부분에서 바로 

<context:component-scan base-package="com.javalec.springmvc_ex_project" />

이문장이 스캔을 해줘 base-package = "com.javalec.springmvc_ex_project" 여기안에서 컨트롤러를 찾아줘요, 그러면 이제 저기안을 스캔을 쭈루룩 한다. 컴파일러가 그러다가 컨틀롤러를 찾으면 컨트롤러에 처리요청을 해줘요

그럼이쯤에서 컨트롤러를 보자...

base-package="com.javalec.springmvc_ex_project" 

밑에는 당연하지만  servlet_context.xml 에서 스캔을 한 패키지안에 있는 컨틀롤러이다.

package com.javalec.springmvc_ex_project;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
     
     @RequestMapping("/index")
     public String index() {
           return "index";
     }
     
     @RequestMapping("/view/contents")
     public String contents() {
           return "/view/contents";
     }
}


해당 url에 맞는 컨트롤러를(@RequestMapping에 써져있음 url이) 찾으면 나다 하고 컨트롤러가 VIEW 이름을 알려줌... 
그러면 servlet-context.xml에서  아 기달려하고 VIEW-RESOLVER를 꺼냄 VIEW-RESOLVER에서는  

prefix(/WEB-INF/views/) + VIEW(VIEW이름) + suffix(.jsp)

요렇게해서 다시 DispatcherServlet 한테 보내버림

<beans:bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <beans:property name="viewClass"  value="org.springframework.web.servlet.view.JstlView"/>
       <beans:property name="prefix" value="/WEB-INF/views/"  />
       <beans:property name="suffix" value=".jsp" />
       <beans:property name="order" value="1"/>
</beans:bean>

그러면 view-resolver 가 view페이지를 prefix(/WEB-INF/views/) + VIEW(VIEW이름) + suffix(.jsp) 요거를 통해서 클라이언트에게 제공함.

그리고 resources 파일 같은경우에는 mapping="/resources/**" 매핑 즉 url이 resources라고 들어오면 location이= "/resources/" 에서 찾아주세요 라고 써져있다.

<resources mapping="/resources/**" location="/resources/" />


반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/09   »
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
글 보관함