1. 서버의 web.xml 파일이 먼저 실행 된 후 그 다음으로 프로젝트 안에 있는 web.xml이 실행된다.
서버의 web.xml이 갖고있는 welcomeFileList의 설정을 프로젝트의 web.xml에서 그대로 가져가는 것이다.
(이전의 index.jsp가 wfl인 것이다.)
<!-- 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>
2.프로젝트의 web.xml의 DispatcherServlet라는 애가 url 매핑을 알아서 해준다.
<param-name> : 변수명 <param-value> : 변수 값
3. web.xml 실행하며 <url-pattern> 태그로 url을 받아준다 -> 그 url은 DispatcherServlet이 받아준다
이 때 궁금한 점 1 ) DispatcherServlet이 HomeController를 어떻게 찾았나?
DispatcherServlet은 "handlerMappring"과 함께 url을 받아줄 곳을 찾는다.
HomeController에 있는 @RequestMapping이란 Annotation으로 받아준다.
* value= "url" -> 넘어갈 url 을 입력하여 데이터를 넘긴다.
package com.withtrip.WithTrip;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/myPage.do", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
그래서 HomeController 로 데이터가 넘어오고 HomeController에 있는 serverTime 변수가 HomeController의 return "home"; 에서 이어져서 /WEB-INF/views/home.jsp라는 경로로
보내지는 것이다.
이 때 궁금한 점 2 ) HomeController에서 home.jsp로 어떻게 데이터를 넘겼나?
servlet-context.xml파일을 조회 시
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" /> 라고 되어있는걸 알 수 있음.
즉, 위에 표시된 /WEB-INF/views/home.jsp 라는 경로는 prefix 와 suffix가 합쳐진 경로라는 것
<?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 https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://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="com.withtrip.WithTrip" />
</beans:beans>
ViewResolver는 controller에서 받은 값을 어떤 화면으로 보낼 것인지 경로를 결정해준다.
완성된 경로를 DispatcherServlet으로 다시 보낸다.
-> home.jsp 에 이동하여 출력
[ 파일 구조 ]
src/test/resources - > 해당 부분에는 설정파일을 집어넣을 것
webapp/resources -> 이미지, jsp, css 등 자원파일을 집어넣을 것
'back-end > Spring framework' 카테고리의 다른 글
Spring 03) parameter 전송받기 (0) | 2022.06.22 |
---|---|
Spring 02) Spring의 구조이해 및 설정 (0) | 2022.06.22 |
Spring ) 프로젝트 환경 설정 (0) | 2022.06.16 |