Spring Web MVC framework
- Model-View-Controller (MVC) architecture
- Model : POJO
- View : Rendering HTML(UI), JSON(Data)
- Controller : Handle request and build model and pass to view
WebApplicationContext extends ApplicationContext
DispatcherServlet
- http request
- handle request
- Controller
- View Resolver
- View
- http response
@Controller
vs @RestController
@Controller
๋ ์ฃผ๋ก Web ํ์ด์ง์ ์ปจํธ๋กค๋ฌ์์ ์ฌ์ฉ
- Web ํ์ด์ง์ฉ ์ปจํธ๋กค๋ฌ๋ ํ
ํ๋ฆฟ ์์ง View๋ JSP๋ก ์ ํ ์๋ต์ HTML์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์๋์ ๋ฐํ๊ฐ์ View ์ ํ ๋์์ ์ง์ ํ๋ ๋ฐ ์ฌ์ฉ
@RestController
๋ Json์ด๋ XML ๋ฑ์ ๋ฐํ WebAPI ์ฉ ์ปจํธ๋กค๋ฌ๋ก ์ฌ์ฉ
- View๋ก ์ ํํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฉ์๋์ ๋ฐํ๊ฐ์ ์๋ต(response)์ ๋ด์ฉ(content) ํ์
web.xml
<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.okdevtv.spring" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
package com.okdevtv.spring;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
related
ref