1. 모든 요청은 front controller로 동작하는 DispatcherServlet에 전달된다. Client가 요청을 Web Server로 보내면, Web Server는 Servlet container로 전달하고 Servlet container 쓰레드가 생성된다. 생성된 쓰레드에서 DispatcherServlet service()가 호출된다.
2. DispatcherServlet는 XML 파일에서 요청에 대한 HandlerMapping 엔트리를 찾는다. HandlerMapping에서는 요청에 대한 매핑된 Controller를 확인한다.
3. HandlerAdapter에 의해 매핑된 Controller에 요청이 전달된다.
4. Controller는 요청에 대한 view component를 반환한다.
5. DispatcherServlet은 XML 파일에서 view component에 대한 ViewResolver 찾고, ViewResolver는 view component의 구체적인 경로를 반환한다.
6. DispatcherServlet은 view component를 호출한다.
위 세 가지 dependency를 필요로 하지만, Maven을 사용하는 경우 Maven dependencies로 해당 파일들이 다운로드되기 때문에 별도로 다운로드하지 않아도 된다.
<?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 https://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>
<?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="org.smu.blood" />
</beans:beans>
다음으로 서로 다른 view 페이지에서 redirect 하는 방법을 설명한다.
프로젝트 파일 구조
HomeController.java
package org.smu.blood;
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);
@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping("/mid")
public String redirect() {
return "page";
}
@RequestMapping("/final")
public String redirect2() {
return "final";
}
}
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<a href="mid">Click here</a>
</body>
</html>
page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<a href="final">Finally</a>
</body>
</html>
final.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>final</title>
</head>
<body>
<p>Spring MVC redirect between view component test</p>
</body>
</html>
프로젝트 파일 구조
HomeController.java
package org.smu.blood;
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);
@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping("/mid1")
public String redirect() {
return "page";
}
}
HomeController2.java
package org.smu.blood;
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);
@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping("/mid1")
public String redirect() {
return "page";
}
}
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<a href="mid1">Click here 1</a>
<a href="mid2">Click here 2</a>
</body>
</html>
page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<p>Multiple Controller Test #1</p>
</body>
</html>
final.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<p>Multiple Controller Test #1</p>
</body>
</html>
프로젝트 실행 결과
JWT (JSON Web Token) (0) | 2022.01.28 |
---|---|
[Spring] Android, Spring 연동 (retrofit2) (1) | 2022.01.24 |
[Spring] Spring, MongoDB 연동 (Spring Data MongoDB) (0) | 2022.01.20 |
[spring] Spring MVC: Model Interface, RequestParam Annotation, Spring MVC form (0) | 2022.01.19 |
[Spring] Eclipse Spring 개발 환경 구축 (0) | 2022.01.17 |