์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- sql
- ๋น๋๊ธฐ
- Porject
- ajax
- ์ฐธ๊ณ ๋ธ๋ก๊ทธ
- https://m.blog.naver.com/tt2t2am1118/221010125300
- json
- mysql
- object
- JS #ํ๋ก์ ํธ
- addEventListener
- await
- webpack
- async
- promise
- ๊ฒ์
- db
- ํผํ
- ๋๊ธฐ
- slow and steady
- setTimeout()
- database
- callback
- https://youtube.com/playlist?list=PLuHgQVnccGMA5836CvWfieEQy0T0ov6Jh&si=FTaYv8m21EhO-A2K
- execCommand
- prj
- Project
- eport
- js
- Import
- Today
- Total
C-log
3week - MVC ๋ณธ๋ฌธ
* ๋๋ฉด ์์ ๋ด์ฉ
model
view
controller
dispatcher servlet
contextloadlistener
Maven์ ์ฌ์ฉํ๋ ์ด์ ์ ์ฅ์
* ์ฑ๊ธํค ํจํด์ ์์์ผํ๋ค.
# MVC ์์ฑ(Controller&View)
### pom.xml์ ๋ฌด์์ธ๊ฐ
* HomeController.java
package kr.ac.hansung.cse.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HomeController {
//private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private static final Logger logger = LoggerFactory.getLogger("kr.ac.hansung.controller.HomeController");
// @RequestMapping(value = "/", method = RequestMethod.GET)
@GetMapping("/")
public String home(HttpServletRequest request) {
String url = request.getRequestURL().toString();
String clientIPaddr = request.getRemoteAddr();
logger.info("Request URL: {}, Client IP: {}", url, clientIPaddr);
return "home";
}
}
์์ ์ฝ๋๋ ์คํ๋ง MVC ์น ์ ํ๋ฆฌ์ผ์ด์
์์ ํ ํ์ด์ง๋ฅผ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ์
๋๋ค. ๊ฐ ๋ถ๋ถ์ ๋ํ ์ค๋ช
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
* @Controller - ์ด๋
ธํ
์ด์
ํด๋น ํด๋์ค๊ฐ ์คํ๋ง MVC์ ์ปจํธ๋กค๋ฌ์์ ๋ํ๋ธ๋ค. ์ด๋ ์ถํ์ appServlet์์ Controller ํด๋์ค๋ฅผ ํ์ํ ๋ ์ค์ํ ์ญํ ์ ํ๋ค.
* @GetMapping("/") - ์ด๋
ธํ
์ด์
?
HTTP GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ค. ์ด ๋ฉ์๋๋ "/" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ค.
* HttpServletRequest request
ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์์ฒญ์ ๋ฐ์๋ค์ด๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ฅผ ํตํด ์์ฒญ URL๊ณผ ํด๋ผ์ด์ธํธ์ IP ์ฃผ์๋ฅผ ๊ฐ์ ธ์จ๋ค.
* Logger logger
๋ก๊ทธ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ Logger ๊ฐ์ฒด์
๋๋ค. ์ฌ๊ธฐ์๋ SLF4J ๋ก๊น
API๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
* logger.info("Request URL: {}, Client IP: {}", url, clientIPaddr)
๋ก๊ทธ์ ์์ฒญ URL๊ณผ ํด๋ผ์ด์ธํธ IP ์ฃผ์๋ฅผ ์ถ๋ ฅํ๋ค.
* return "home";
"home"์ด๋ผ๋ ๋ทฐ ์ด๋ฆ์ ๋ฐํํ์ฌ ํด๋น ๋ทฐ๋ฅผ ํ์ํ๋ค. ์คํ๋ง MVC๋ ์ด ๋ทฐ ์ด๋ฆ์ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ๋ทฐ ํ์ผ์ ์ฐพ์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํ๋ค.
๋ฐ๋ผ์ ์ด ์ปจํธ๋กค๋ฌ๋ "/" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ GET ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ์์ฒญ URL๊ณผ ํด๋ผ์ด์ธํธ์ IP ์ฃผ์๋ฅผ ๋ก๊ทธ์ ์ถ๋ ฅํ ํ, "home"์ด๋ผ๋ ๋ทฐ๋ฅผ ํ์ํ๋ค. ์๋ ์ฝ๋๋ ๋ฐํ๋ home ๋ทฐ์ ํด๋น๋๋ jsp์ด๋ค. jsp๋ ํฌ๊ฒ ์ด๋ ค์์ด ์๊ธฐ์ ํจ์ค ํ๋๋ก ํ์.
* home.jsp
<%--
Created by IntelliJ IDEA.
User: nykim
Date: 2022/12/13
Time: 12:55 ์ค์
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<p><a href="${pageContext.request.contextPath}/offers"/>Show Current offers</p>
</body>
</html>
* helloSpring-1.0-SNAPSHOT.war
helloSpring-1.0-SNAPSHOT.war ํ์ผ์ Maven ๋๋ Gradle๊ณผ ๊ฐ์ ๋น๋ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์์นด์ด๋ธ(WAR) ํ์ผ์ด๋ค. ์ด ํ์ผ์ ์คํ๋ง ํ๋ ์์ํฌ ๋๋ ๋ค๋ฅธ ์น ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ฐํ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌ๋ฅผ ์ํด ์ฌ์ฉ๋๋ค.
## ์ง๊ธ๊น์ง์ ํจํค์ง ํ์ผ ๊ตฌ์กฐ
์ง๊ธ๊น์ง ํ์ผ์ ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๋ฉด controller ํจํค์ง ํ์ผ๊ณผ dao ํจํค์ง ํ์ผ ๋ง์ง๋ง์ผ๋ก service ํจํค์ง ํ์ผ๋ก ๊ตฌ์ฑ์ด ๋์ด ์๋ค. ์ด๋ฌํ ๊ตฌ์กฐ๋ ์๋ ์ด๋ฏธ์ง์ ๊ฐ์ด ์ด๋ฃจ์ด์ ธ ์๋ค.
์ง๊ธ๊น์ง์ ํ์ผ ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๋ฉด ๊ฐ๊ฐ์ ์ฑ ์์ ๋ถ๋ช ํ ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ๊ฒ ํ๊ณ ์ ์ง๋ณด์์ ํ์ฅ์ฑ์ ํฅ์์ํค๋ ๋ฐ ์ค์ํ ์ญํ ์ ํ๋ค. ์ด์ ๊ตฌ์ฒด์ ์ธ ์ค๋ช ์ ์๋ appServlet์ ํตํด ์ค๋ช ํ๊ฒ ๋ค.
appServlet ์ค์ ํ์ผ์ ์ฃผ๋ก ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ์ธ DispatcherServlet์ ๊ตฌ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ ์ด๋ ์น ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ๋ผ์ฐํ ํ๋ ์ญํ ์ ์ํํ๋ค.
* appServlet : servlet-context.xml, doa-context.xml, service-context.xml
<?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="kr.ac.hansung.cse.controller" />
</beans:beans>
<context:component-scan base-package="kr.ac.hansung.cse.controller" />
@Controller
public class HomeController {
...
servlet-context.xml ์ฝ๋๋ Spring MVC ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ค์ ์ ๋ด๊ณ ์์ผ๋ฉฐ ์ฌ๊ธฐ์ <context:component-scan base-package="kr.ac.hansung.cse.controller" /> ๋ถ๋ถ์ด ํต์ฌ์ ์ธ ์ญํ ์ ํ๋ค. <context:component-scan> ํ๊ทธ๋ Spring Framework์๊ฒ ์ง์ ๋ controller ํจํค์ง ๋ด์์ ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ค์บํ๋๋ก ์ง์ํ๋ค. ์ด๋ฅผ ํตํด ํด๋น ํด๋์ค๋ค์ Spring์ ApplicationContext์ ๋น(Bean)์ผ๋ก ๋ฑ๋กํ๊ฒ ๋๋ค. ์ด๋ ๊ฒ ์น ์์ฒญ์ ์ฒ๋ฆฌํ๋ controller๋ก ์ธ์๋์ด ํด๋น ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋๋ค์ด ์น ์์ฒญ์ ์๋ตํ ์ ์๊ฒ ๋๋ค.
### dispatch๋?
### Servlet์ด๋?
Servlet์ ์๋ฐ๋ฅผ ์ฌ์ฉํ์ฌ ์น ํ์ด์ง๋ฅผ ๋์ ์ผ๋ก ์์ฑํ๋ ์๋ฒ ์ธก ํ๋ก๊ทธ๋จ ๋๋ ์๋ฐ ํด๋์ค์ด๋ค. ์น ์๋ฒ์์ ๋์ํ๋ฉฐ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ๋ผ ์ฒ๋ฆฌ ํ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋๋ ค์ฃผ๋ ์ญํ ์ ํ๋ค. ์๋ธ๋ฆฟ์ ์ฃผ๋ก HTML, XML ๋ฑ์ ์์ฑํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋๊ณผ ๊ฐ์ ๋น์ฆ๋์ค ๋ก์ง ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ค. ์๋ธ๋ฆฟ์ ์๋ฐ EE(Enterprise Edition) ์ฌ์์ ์ผ๋ถ๋ก ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๊ธฐ ์ํ ์๋ฐ์ ํ์ค ๊ธฐ์ ์ด๋ค. ์๋ธ๋ฆฟ์ ์ฌ์ฉํ๋ฉด ํ๋ซํผ ๋ ๋ฆฝ์ ์ธ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ ์ ์๋ค.
### dispatchServlet์ด๋?
DispatcherServlet์ ์คํ๋ง MVC(๋ชจ๋ธ-๋ทฐ-์ปจํธ๋กค๋ฌ) ํ๋ ์์ํฌ์์ ์ค์ฌ์ ์ธ ์ญํ ์ ํ๋ ์๋ธ๋ฆฟ์ผ๋ก ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ค์ด์ค๋ ๋ชจ๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ค. ์คํ๋ง ์น MVC์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๋ก์ DispatcherServlet์ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ ์์ฒญ์ ์ ์ ํ ํธ๋ค๋ฌ ๋๋ ์ปจํธ๋กค๋ฌ์ ์ ๋ฌํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํ๋ ์ญํ ์ ํ๋ค.
* root-context.xml์ ์ ๊ฑฐ
root-context.xml ํ์ผ์ ๋ฃจํธ ์ปจํ ์คํธ๋ฅผ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ๊ทธ๋ฌ๋ ์ต๊ทผ์ ์คํ๋ง ํ๋ ์์ํฌ ๋ฒ์ ์์๋ root-context.xml์ ์ฌ์ฉํ์ง ์๊ณ Java ๊ธฐ๋ฐ์ ์ค์ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ฃจํธ ์ปจํ ์คํธ๋ฅผ ์ค์ ํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค. Java ๊ธฐ๋ฐ์ ์ค์ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด XML ํ์ผ๋ณด๋ค ์ ์ฐํ๊ณ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์ค์ ์ ์กฐ์ํ ์ ์๋ค.
* web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 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/appServlet/service-context.xml
/WEB-INF/spring/appServlet/doa-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 ํ์ผ์ ์คํ๋ง MVC ์น ์ ํ๋ฆฌ์ผ์ด์
์ web.xml ์ค์ ํ์ผ์ด๋ค. ์ด ํ์ผ์ ์น ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฐฐ์น ๋ฐ ์ด๊ธฐํ๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ๊ฐ ์์์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค
* web-app
์ด ์์๋ ์ด XML ํ์ผ์ด ์น ์ ํ๋ฆฌ์ผ์ด์
์ ์ค์ ํ์ผ์์ ๋ํ๋ธ๋ค.
* context-param
์ด ์์๋ ์น ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฃจํธ ์ปจํ
์คํธ๋ฅผ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ฌ๊ธฐ์๋ contextConfigLocation์ด๋ผ๋ ์ปจํ
์คํธ ํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํ์ฌ service-context.xml ๋ฐ dao-context.xml ํ์ผ์ ์ง์ ํ๊ณ ์์ต๋๋ค.
* listener
ContextLoaderListener ํด๋์ค๋ฅผ ๋ฑ๋กํ์ฌ ์น ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฃจํธ ์ปจํ
์คํธ๋ฅผ ์์ฑํ๋ค.
* servlet
์ด ์์๋ ์คํ๋ง MVC ์ ํ๋ฆฌ์ผ์ด์
์ ์๋ธ๋ฆฟ์ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ฌ๊ธฐ์๋ DispatcherServlet์ ๋ฑ๋กํ๊ณ servlet-context.xml ํ์ผ์ ๋ก๋ํ๋๋ฐ ์ฌ์ฉ๋๋ค.
* servlet-mapping
์๋ธ๋ฆฟ ๋งคํ์ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด ๊ฒฝ์ฐ / ๊ฒฝ๋ก์ ๋ํ ๋ชจ๋ ์์ฒญ์ appServlet ์๋ธ๋ฆฟ์ ๋งคํํ๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์คํ๋ง MVC ์น ์ ํ๋ฆฌ์ผ์ด์
์ด ์ด๊ธฐํ๋๊ณ ์ค์ ๋๋ฉฐ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ค๋น๊ฐ ์๋ฃ๋๋ค.
์ง๊ธ๊น์ง์ ํด๋ ํ์ผ ๊ตฌ์ฑ์ ์๋์ ๊ฐ๋ค.
# MVC ๊ตฌํ(Model&Controller&View)
๋ชจ๋ธ๊ตฌํ
* Offer.java
package kr.ac.hansung.cse.model;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@NoArgsConstructor
public class Offer {
private int id;
private String name;
private String email;
private String text;
}
์์ ์ฝ๋๋ Java ํด๋์ค์ธ Offer๋ฅผ ์ ์ํ๊ณ ์๋ค. ์ด ํด๋์ค๋ ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์ฌ์ฉ์ ์
๋ ฅ๊ณผ ๊ฐ์ ์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ํํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค. ๊ฐ ์ด๋
ธํ
์ด์
๋ค์ ์ฃผ์๊น๊ฒ ์ดํด๋ณด์
* @Getter
Lombok ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์๋์ผ๋ก ํ๋์ ๋ํ getter ๋ฉ์๋๋ฅผ ์์ฑํ๋ค. ์ด๊ฒ์ id, name, email, text ํ๋์ ๋ํ getter ๋ฉ์๋๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
* @Setter
Lombok ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์๋์ผ๋ก ํ๋์ ๋ํ setter ๋ฉ์๋๋ฅผ ์์ฑํ๋ค. ์ด๊ฒ์ id, name, email, text ํ๋์ ๋ํ setter ๋ฉ์๋๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
* @ToString
Lombok ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์๋์ผ๋ก toString() ๋ฉ์๋๋ฅผ ์์ฑํ๋ค. ์ด ๋ฉ์๋๋ ๊ฐ์ฒด์ ํ๋ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ค.
* @NoArgsConstructor
Lombok ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์ธ์๋ฅผ ๋ฐ์ง ์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
* private int id
Offer ๊ฐ์ฒด์ id๋ฅผ ์ ์ฅํ๋ ํ๋์ด๋ค.
* private String name
Offer ๊ฐ์ฒด์ ์ด๋ฆ์ ์ ์ฅํ๋ ํ๋์ด๋ค.
* private String email
Offer ๊ฐ์ฒด์ ์ด๋ฉ์ผ ์ฃผ์๋ฅผ ์ ์ฅํ๋ ํ๋์ด๋ค.
* private String text
Offer ๊ฐ์ฒด์ ํ
์คํธ๋ฅผ ์ ์ฅํ๋ ํ๋์ด๋ค.
์ด ํด๋์ค์ ์ญํ ์ ์ฃผ๋ก ๋ฐ์ดํฐ๋ฅผ ํํํ๊ณ ์ ์ฅํ๋ ๋ฐ์ ์๋ค. ์ด ํด๋์ค์ ์ธ์คํด์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ
์ด๋ธ์ ๋ ์ฝ๋๋ฅผ ๋ํ๋ผ ์ ์๊ณ ์น ์ดํ๋ฆฌ์ผ์ด์
์์ ํผ ๋ฐ์ดํฐ๋ฅผ ๋ํ๋ผ ์๋ ์๋ค. Getter์ Setter ๋ฉ์๋๋ ํ๋์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๊ฑฐ๋ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํด์ฃผ๊ณ toString() ๋ฉ์๋๋ ๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ํํํ์ฌ ๋ก๊น
๋ฐ ๋๋ฒ๊น
๋ชฉ์ ์ผ๋ก ์ฌ์ฉ๋ ์ ์๋ค.
* OfferController.java
package kr.ac.hansung.cse.controller;
import kr.ac.hansung.cse.model.Offer;
import kr.ac.hansung.cse.service.OfferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class OfferController {
@Autowired
private OfferService offerService;
//offerDao๋ก ๋ถํฐ mysql๋ค์ ์กฐ์ ์ปจํธ๋กค ํ๋ค.
@GetMapping("/offers")
public String showOffer(Model model){
List<Offer> offers = offerService.getAllOffers();
model.addAttribute("id_offers", offers);
return "offers";
}
}
์์ ์ฝ๋๋ ์คํ๋ง MVC ์น ์ ํ๋ฆฌ์ผ์ด์
์์ Offer ๊ฐ์ฒด๋ฅผ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ์ธ OfferController๋ฅผ ์ ์ํ๊ณ ์๋ค. ์ด ์ปจํธ๋กค๋ฌ์ ์ญํ ๊ณผ ์ฝ๋๋ฅผ ๋ถ์ํด๋ณด์.
* @Controller
ํด๋น ํด๋์ค๊ฐ ์คํ๋ง MVC์ ์ปจํธ๋กค๋ฌ์์ ๋ํ๋ธ๋ค.
* @Autowired private OfferService offerService;
OfferService ํ์
์ ๋น์ ์๋์ผ๋ก ์ฃผ์
๋ฐ๋๋ค. ์ด๊ฒ์ ์คํ๋ง ํ๋ ์์ํฌ์ ์ํด OfferService์ ๊ตฌํ์ฒด๊ฐ ์์ฑ๋๊ณ offerService ํ๋์ ์ฃผ์
๋๋ค.
* @GetMapping("/offers")
HTTP GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ค. ์ด ๋ฉ์๋๋ "/offers" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ค.
* public String showOffer(Model model)
Model ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋ ๋ฉ์๋์ด๋ค. ์ด ๋ชจ๋ธ ๊ฐ์ฒด๋ ์ปจํธ๋กค๋ฌ์์ ๋ทฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
* List<Offer> offers = offerService.getAllOffers();
offerService๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ Offer ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค. ์ด๋ ์๋น์ค ๊ณ์ธต์ ์๋ OfferService ๊ฐ์ฒด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ค๋ฅธ ๊ณณ์์ Offer ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ญํ ์ ํ๋ค.
* model.addAttribute("id_offers", offers);
๋ชจ๋ธ์ "id_offers"๋ผ๋ ์ด๋ฆ์ผ๋ก Offer ๋ชฉ๋ก์ ์ถ๊ฐํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋ทฐ์์ ์ด ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์๊ฒ ๋๋ค.
* return "offers";
"offers"๋ผ๋ ๋ทฐ ์ด๋ฆ์ ๋ฐํํ๋ค. ์ด๋ ์คํ๋ง MVC๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ด ์ด๋ฆ์ ๊ฐ์ง๋ ๋ทฐ๋ฅผ ์ฐพ์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํ๋ค.
๋ฐ๋ผ์ OfferController๋ /offers ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ GET ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ด๋ฅผ ํตํด Offer ๊ฐ์ฒด๋ฅผ ์กฐํํ์ฌ ๋ทฐ๋ก ์ ๋ฌํ๋ ์ญํ ์ ํ๋ค. ์ด๋ฌํ ๋์์ธ์ MVC (Model-View-Controller) ํจํด์ ๋ฐ๋ฅด๋ฉฐ ๊ฐ ์์์ ์ญํ ์ ๋ถ๋ฆฌํ์ฌ ์ฝ๋๋ฅผ ๋ณด๋ค ๊ตฌ์กฐํํ๊ณ ์ ์ง๋ณด์ํ๊ธฐ ์ฝ๊ฒ ๋ง๋ ๋ค.
* offers.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: rdwoo1024
Date: 2024-03-27
Time: ์คํ 5:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach var="offer" items="${id_offers}">
<p><c:out value="${offer}"></c:out></p>
</c:forEach>
</body>
</html>
์์ ์ฝ๋๋ JSP ํ์ผ์ธ offer.jsp์ด๋ค. ์ด ํ์ผ์ Offer ๊ฐ์ฒด์ ๋ชฉ๋ก์ ์ถ๋ ฅํ๋ ์ญํ ์ ํ๋ค. ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์ญํ ์ ์ดํด๋ณด์.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL(Core) ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์ธํ๋ค. ์ด๋ฅผ ํตํด JSP ํ์ผ์์ JSTL์ ์ ์ด ๊ตฌ๋ฌธ ๋ฐ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
ํด๋น JSP ํ์ด์ง์ ์ฝํ
์ธ ์ ํ์ HTML๋ก ์ค์ ํ๊ณ ๋ฌธ์ ์ธ์ฝ๋ฉ์ UTF-8๋ก ์ค์ ํ๋ค.
<c:forEach var="offer" items="${id_offers}">
JSTL์ <c:forEach> ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ${id_offers}์ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ณตํ๋ฉฐ ์ฒ๋ฆฌํ๋ค. ${id_offers}๋ ์ปจํธ๋กค๋ฌ์์ ๋ชจ๋ธ์ ์ถ๊ฐ๋ Offer ๊ฐ์ฒด๋ค์ ๋ฆฌ์คํธ๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
<p><c:out value="${offer}"></c:out></p>
๋ฐ๋ณต๋ฌธ ์์์ Offer ๊ฐ์ฒด๋ฅผ ์ถ๋ ฅํ๋ ๋ถ๋ถ์ด๋ค. <c:out> ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ${offer} ๊ฐ์ ์ถ๋ ฅํ๋ค. ์ด๋ฅผ <p> ํ๊ทธ๋ก ๋๋ฌ์ธ์ ๊ฐ Offer ๊ฐ์ฒด๋ฅผ ๋จ๋ฝ์ผ๋ก ๋ํ๋ธ๋ค.
<title>Title</title>
HTML ๋ฌธ์์ ์ ๋ชฉ์ ๋ํ๋ด๋ ๋ถ๋ถ์ด๋ค.
<body>
HTML ๋ฌธ์์ ๋ณธ๋ฌธ์ ์์ํ๋ ํ๊ทธ์ด๋ค.
<html>, <head>, <body>
HTML ๋ฌธ์์ ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋ ํ๊ทธ์ด๋ค.
๋ฐ๋ผ์ offer.jsp ํ์ผ์ Offer ๊ฐ์ฒด ๋ชฉ๋ก์ ๋ฐ๋ณตํ์ฌ ์ถ๋ ฅํ๋ ์ญํ ์ ํ๋ค. ์ด๋ฅผ ํตํด Offer ๊ฐ์ฒด์ ์ ๋ณด๋ฅผ ์น ํ์ด์ง ์์ ํ์ํ ์ ์๋ค
* OfferService.java
package kr.ac.hansung.cse.service;
import kr.ac.hansung.cse.dao.OfferDao;
import kr.ac.hansung.cse.model.Offer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OfferService {
@Autowired
private OfferDao offerDao;
public List<Offer> getAllOffers(){
return offerDao.getOffers();
}
}
์์ ์ฝ๋๋ OfferService๋ผ๋ ์๋น์ค ํด๋์ค์ด๋ค. ์ด ํด๋์ค๋ ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ์ฌ Offer ๊ฐ์ฒด์ ๋ํ ๋น์ฆ๋์ค ๋ก์ง์ ์ฒ๋ฆฌํ๋ค. ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์ญํ ์ ์ค๋ช
ํด๋ณด๊ฒ ํ๋ค
@Service
ํด๋น ํด๋์ค๊ฐ ์คํ๋ง์ ์๋น์ค๋ก ์ธ์๋๋๋ก ํ๋ ์ด๋
ธํ
์ด์
์ด๋ค. ์ด๋ ์คํ๋ง ์ปจํ
์ด๋๊ฐ ํด๋น ํด๋์ค๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ ํ์ํ ๊ณณ์์ ์ฃผ์
ํ์ฌ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ค.
@Autowired private OfferDao offerDao;
OfferDao ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋น์ ์๋์ผ๋ก ์ฃผ์
๋ฐ๋๋ค. ์ด๊ฒ์ ์คํ๋ง ํ๋ ์์ํฌ์ ์ํด OfferDao์ ๊ตฌํ์ฒด๊ฐ ์์ฑ๋๊ณ offerDao ํ๋์ ์ฃผ์
๋๋ค.
public List<Offer> getAllOffers()
๋ชจ๋ Offer ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ DAO ๊ณ์ธต์ ์๋ OfferDao ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ Offer ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
return offerDao.getOffers();
OfferDao ๊ฐ์ฒด์ getOffers() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ชจ๋ Offer ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค. ์ด ๋ฉ์๋๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ Offer ์ ๋ณด๋ฅผ ์กฐํํ์ฌ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ค.
๋ฐ๋ผ์ OfferService ํด๋์ค๋ ์ฃผ๋ก DAO ๊ณ์ธต๊ณผ ์ํธ ์์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ Offer ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ญํ ์ ํ. ์ด๋ฅผ ํตํด ์ปจํธ๋กค๋ฌ๋ ๋ค๋ฅธ ์๋น์ค ๊ณ์ธต์์ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๋ค. ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๋น์ฆ๋์ค ๋ก์ง์ ์๋น์ค ๊ณ์ธต์ ๋ถ๋ฆฌํจ์ผ๋ก์จ ์ฝ๋์ ์์ง์ฑ์ ๋์ด๊ณ ์ ์ง๋ณด์์ฑ์ ํฅ์์ํจ๋ค.
*OfferDao.java
package kr.ac.hansung.cse.dao;
import kr.ac.hansung.cse.model.Offer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class OfferDao {
private JdbcTemplate jdbcTemplate; // psa(portable service abstraction, sql(x) api
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int getRowCount() {
String sqlStatement= "select count(*) from offers";
return jdbcTemplate.queryForObject(sqlStatement, Integer.class);
}
//query and return a single object
public Offer getOffer(String name) {
String sqlStatement= "select * from offers where name=?";
return jdbcTemplate.queryForObject(sqlStatement, new Object[] {name},
new RowMapper<Offer>() {
@Override
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer= new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setEmail(rs.getString("email"));
offer.setText(rs.getString("text"));
return offer;
}
});
}
//query and return multiple objects
// cRud method
public List<Offer> getOffers() {
String sqlStatement= "select * from offers";
return jdbcTemplate.query(sqlStatement, new RowMapper<Offer>() {
@Override
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer= new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setEmail(rs.getString("email"));
offer.setText(rs.getString("text"));
return offer;
}
});
}
// Crud method
public boolean insert(Offer offer) {
String name= offer.getName();
String email= offer.getEmail();
String text = offer.getText();
String sqlStatement= "insert into offers (name, email, text) values (?,?,?)";
return (jdbcTemplate.update(sqlStatement, new Object[] {name, email, text}) == 1);
}
// crUd method
public boolean update(Offer offer) {
int id = offer.getId();
String name= offer.getName();
String email= offer.getEmail();
String text = offer.getText();
String sqlStatement= "update offers set name=?, email=?, text=? where id=?";
return (jdbcTemplate.update(sqlStatement, new Object[] {name, email, text, id}) == 1);
}
//cruD method
public boolean delete(int id) {
String sqlStatement= "delete from offers where id=?";
return (jdbcTemplate.update(sqlStatement, new Object[] {id}) == 1);
}
}
OfferDao ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ์์ฉํ๊ธฐ ์ํ DAO(Data Access Object) ํด๋์ค๋ก์, ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ CRUD(Create, Read, Update, Delete) ์์
์ ์ํํ๋ค. ์ด ํด๋์ค๋ JDBC(Java Database Connectivity)๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์ฒ๋ฆฌํ๋ค.
@Repository
ํด๋น ํด๋์ค๊ฐ ์คํ๋ง์ ๋น์ผ๋ก ๋ฑ๋ก๋๋ ๋ฆฌํฌ์งํ ๋ฆฌ ํด๋์ค์์ ๋ํ๋ธ๋ค. ์ด๋ ์คํ๋ง์ด ํด๋น ํด๋์ค๋ฅผ ์ปดํฌ๋ํธ ์ค์บ์ ํตํด ๋น์ผ๋ก ๋ฑ๋กํ๊ณ , ํ์ํ ๊ณณ์์ ์ฃผ์
ํ์ฌ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ค.
private JdbcTemplate jdbcTemplate;
JDBC๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ์์ฉํ๊ธฐ ์ํ JdbcTemplate ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ค.
public void setDataSource(DataSource dataSource) { ... }
DataSource ๊ฐ์ฒด๋ฅผ ๋ฐ์์ JdbcTemplate ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๋ฉ์๋์ด๋ค. ์ด ๋ฉ์๋๋ ์คํ๋ง์ ์์กด์ฑ ์ฃผ์
(Dependency Injection)์ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์ค์ ํ๋ค.
public int getRowCount() { ... }
๋ฐ์ดํฐ๋ฒ ์ด์ค ํ
์ด๋ธ์ ํ ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ด๋ค.
public Offer getOffer(String name) { ... }
์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ํน์ Offer ๊ฐ์ฒด๋ฅผ ์กฐํํ๋ ๋ฉ์๋์ด๋ค.
public List<Offer> getOffers() { ... }
๋ชจ๋ Offer ๊ฐ์ฒด์ ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ ๋ฉ์๋์ด๋ค.
public boolean insert(Offer offer) { ... }
์๋ก์ด Offer ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฝ์
ํ๋ ๋ฉ์๋์ด๋ค.
public boolean update(Offer offer) { ... }
๊ธฐ์กด์ Offer ๊ฐ์ฒด๋ฅผ ์
๋ฐ์ดํธํ๋ ๋ฉ์๋์ด๋ค.
public boolean delete(int id) { ... }
ํน์ id๋ฅผ ๊ฐ์ง Offer ๊ฐ์ฒด๋ฅผ ์ญ์ ํ๋ ๋ฉ์๋์ด๋ค.
์ด๋ฌํ ๋ฉ์๋๋ค์ ํตํด OfferDao ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ํธ์์ฉ์ ์ถ์ํํ๊ณ ์๋น์ค๋ ์ปจํธ๋กค๋ฌ ๋ฑ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ์ฌ CRUD ์์
์ ์ํํ ์ ์๋๋ก ํ๋ค. ์ด๋ฅผ ํตํด ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋ฐ์ดํฐ ์ก์ธ์ค ๋ก์ง์ ๋ถ๋ฆฌํ๊ณ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ํฅ์์ํจ๋ค.
# MVC ํตํฉํ ์คํธ(MYSQL)
ํด๋น ํํธ๋ ๊ฐ์๋ฅผ ํตํด ์ ๋ฆฌํด ๋๋ ๊ฒ ๋ณด๋จ ๋ณด๋ ๊ฒ์ด ์ฝ๋ค. ๋ฐ๋ก ์ ๋ฆฌํด ๋์ง ์์์ผ๋ ๊ฐ ๊ฐ์ธ๋ค ๋ง๋ค offfer ํํธ๋ฅผ ์ค์ฌ์ผ๋ก ์๋ฃ๋ฅผ ์ฐพ์ ์ค์ต์ ํด๋ณด๊ธฐ ๋ฐ๋๋ค.
##์ค๊ฐ๊ณ ์ฌ๋ฅผ ์ํ ์ด๋ก ์ ๋ฆฌ
Controller์ View์ ๊ดํด์
* Model
Model์ Controller๊ฐ ์ํํ ๊ฒฐ๊ณผ๋ฌผ์ ์ ์ฅํ๋ ๊ณณ์ด๋ค. Controller๊ฐ Model์ ๊ฒฐ๊ณผ๋ฅผ ๋๊ฒจ ์ฃผ๋ฉด View์์ ๊ฒฐ๊ณผ๋ฌผ์ ๊บผ๋ด์ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๋ฐฉ์์ด ๋๊ฒ ๋ค. Model์์๋ POJO๊ฐ์ฒด๊ฐ ์์ฑ์ด ๋๋ค. Named Object๊ฐ ์งํฉ๋์ด ์๋ ๊ฒ์ด Model์ด๋ค. (Named Object๋ ์คํ๋ง MVC์ Model์์ ์ฌ์ฉ๋๋ ๋ฐ์ดํฐ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฉ์ด๋ก ์ดํดํ ์ ์๋ค.)
* Model์ ์ด๋ป๊ฒ ๊ตฌํํ๋๊ฐ
์ธ๊ฐ์ง๊ฐ ์๋๋ฐ java.util.map, Model, ModelMap ๋ฐฉ์์ด ์๋ค.
1. public String getGreeting(Map<String, Object> model ) { ... }
์ด ๋ฐฉ๋ฒ์ java.util.Map ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ ๋ฌํ๋ค. ๋ฉ์๋ ๋ด์์ ๋ฐ์ดํฐ๋ model ๋งค๊ฐ๋ณ์์ ์ถ๊ฐ๋๋ค. ์ด ๋ฐฉ๋ฒ์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ด๊ณ ๋ฒ์ฉ์ ์ธ ๋ฐฉ๋ฒ ์ค ํ๋์ด๋ฉฐ ๋ชจ๋ธ์ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ๋ฐ ๋ง์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
์ด ์ฝ๋๋ "/greeting" ์๋ํฌ์ธํธ์ ๋งคํ๋ ๋ฉ์๋์
๋๋ค. ์ด ๋ฉ์๋๋ ์๋น์ค๋ก๋ถํฐ "greeting"์ ๊ฐ์ ธ์์ ๋ชจ๋ธ์ "greeting"์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ์ค์ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ "home"์ด๋ผ๋ ๋ทฐ๋ฅผ ๋ฐํํฉ๋๋ค.
์ฌ๊ธฐ์ model.put("greeting", greeting)์ "greeting"์ด๋ผ๋ ์ด๋ฆ์ผ๋ก service์์ ๊ฐ์ ธ์จ "greeting"์ ๋ชจ๋ธ์ ์ถ๊ฐํ๋ ์ญํ ์ ํฉ๋๋ค. ์ด ๋ชจ๋ธ์ ๋ทฐ์ ์ ๋ฌ๋์ด ํ๋ฉด์ ํ์๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋ทฐ์์๋ "greeting"์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ชจ๋ธ์ ์ค์ ๋ ๊ฐ์ ์ ๊ทผํ์ฌ ํด๋น "greeting"์ ํ์ํ ์ ์์ต๋๋ค.
2. public String getGreeting(Model model ) { ... }
์ด ๋ฐฉ๋ฒ์ ์คํ๋ง ํ๋ ์์ํฌ์ org.springframework.ui.Model ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ ๋ฌํ๋ค. ์ด ์ธํฐํ์ด์ค๋ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ณ ๊ฒ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ฃผ๋ก ์คํ๋ง MVC ์ปจํธ๋กค๋ฌ์์ ๋ง์ด ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ ์ค ํ๋์ด๋ค.
Map์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๊ฐ ๋ชจ๋ธ ์์ฑ์ ์ด๋ฆ์ ๊ฒฐ์ ํด์ผ ํ๋ค. ์ด๋ ์ง๋ฃจํ๊ณ ์ด๋ ค์ด ๊ณผ์ ์ผ ์ ์๋ค. ์๋ฅผ ๋ค์ด, "greeting"์ด๋ผ๋ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ greeting ๋ณ์๋ฅผ ๋ชจ๋ธ์ ์ถ๊ฐํ๋ค.
Model ์ธํฐํ์ด์ค๋ addAttribute()์ ๊ฐ์ ํธ๋ฆฌํ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ Model์ ์ฑ์ฐ๋ ๋ฐ ์ฌ์ฉ๋๋ค. addAttribute()๋ Map์ put()๊ณผ ๋์ผํ์ง๋ง ๋ชจ๋ธ ์์ฑ์ ์ด๋ฆ์ ์๋์ผ๋ก ์์ฑํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ฌ์ ํ ๊ฐ ๋ชจ๋ธ ์์ฑ์ ์ด๋ฆ์ ์๋ ์ ์ผ๋ก ์ง์ ๊ฒฐ์ ํ ์ ์์ง๋ง ์ด๋ฆ์ ์๋์ผ๋ก ์์ฑํ ์๋ ์๋ค.
3. public String getGreeting(ModelMap model ) { ... }
์ด ๋ฐฉ๋ฒ์ org.springframework.ui.ModelMap ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ ๋ฌํ๋ค. ModelMap์ Model๊ณผ ์ ์ฌํ์ง๋ง ์ถ๊ฐ์ ์ธ ํธ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ModelMap์ ๋ด๋ถ์ ์ผ๋ก LinkedHashMap์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๋ฏ๋ก ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋ ์์๋๋ก ์ ์ง๋๋ค.
* Controller
@Controller annotation
@Controller ์ด๋ ธํ ์ด์ ์ ํด๋์ค๊ฐ ์ปจํธ๋กค๋ฌ ์ญํ ์ ํ๋ค๋ ๊ฒ์ ๋ํ๋ธ๋ค. ์ด ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ Spring์๊ฒ ํด๋น ํด๋์ค๋ฅผ ์ปจํธ๋กค๋ฌ๋ก ์ธ์ํ๋๋ก ์ง์ ํ๋ค.
@RequestMapping annotation
@RequestMapping ์ด๋ ธํ ์ด์ ์ ํน์ URL์ ํด๋์ค ์ ์ฒด ๋๋ ํน์ ํธ๋ค๋ฌ ๋ฉ์๋์ ๋งคํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด ๊ฒฝ์ฐ ํด๋น ์ด๋ ธํ ์ด์ ์ home() ๋ฉ์๋๊ฐ URL /์ ๋ํ GET ์์ฒญ์ ์ฒ๋ฆฌํ ๊ฒ์์ ์ง์ ํ๋ค.
@Controller(@Compponet,@Service,@Repository)
@Component, @Service, @Repository๋ Spring ํ๋ ์์ํฌ์์ ๋น์ ์ ์ธํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ์ด๋
ธํ
์ด์
์
๋๋ค. ์ด๋ค์ ๋ชจ๋ @Component ์ด๋
ธํ
์ด์
์ ํน์ํ ํํ์ด๋ฉฐ, ๊ฐ๊ฐ์ ๋ชฉ์ ์ ๋ฐ๋ผ ์ฌ์ฉ๋ฉ๋๋ค.
@Component: Spring ์ ํ๋ฆฌ์ผ์ด์
์์ ๊ตฌ์ฑ ์์๋ฅผ ๋ํ๋ด๋ ์ผ๋ฐ์ ์ธ ์ด๋
ธํ
์ด์
์
๋๋ค. ๋ค๋ฅธ ์ธ๋ถ ์ด๋
ธํ
์ด์
๋ค์ ๋ถ๋ชจ ์ญํ ์ ํฉ๋๋ค. ํด๋์ค๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ Spring ์ปจํ
์ด๋์ ์ํด ๊ด๋ฆฌ๋๊ฒ ํฉ๋๋ค.
@Service: ๋น์ฆ๋์ค ๋ก์ง์ด ํฌํจ๋ ์๋น์ค ๊ณ์ธต์ ํด๋์ค๋ฅผ ๋ํ๋
๋๋ค. ์ฃผ๋ก ์๋น์ค ํด๋์ค์ ์ฌ์ฉ๋๋ฉฐ, ๋น์ฆ๋์ค ๋ก์ง์ ๊ตฌํํ๊ณ ํธ๋์ญ์
๊ด๋ฆฌ ๋ฑ์ ๋ด๋นํฉ๋๋ค.
@Repository: ๋ฐ์ดํฐ ์ก์ธ์ค ๋ก์ง์ ๋ด๋นํ๋ ํด๋์ค๋ฅผ ๋ํ๋
๋๋ค. ์ฃผ๋ก DAO(Data Access Object)๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ํธ์์ฉ์ ๋ด๋นํ๋ ํด๋์ค์ ์ฌ์ฉ๋ฉ๋๋ค. ์ฃผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ๊ณ ์กฐ์ํ๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๋ฉฐ, ์์ธ๋ฅผ ์คํ๋ง์ ์ผ๊ด๋ ์์ธ๋ก ๋ณํํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
์ด ์ธ ๊ฐ์ง ์ด๋
ธํ
์ด์
์ ๋ชจ๋ @Component ์ด๋
ธํ
์ด์
์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ, ๊ฐ๋ฐ์์๊ฒ ์ฝ๋๋ฅผ ๋ ๋ช
ํํ๊ฒ ํํํ๊ณ ์๋ฏธ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค. ๋ํ, ์ปดํฌ๋ํธ ์ค์บ(Component Scan) ์์ Spring์ด ์ด๋ค์ ์ธ์ํ์ฌ ์๋์ผ๋ก ๋น์ผ๋ก ๋ฑ๋กํด์ค๋๋ค.
@Controller(Mapping Requests : Class)
Class : ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์์ ๋๋ค. @Controller ์ ๋ ธํ ์ด์ ์ด ์ง์ ๋ ํด๋์ค๋ ์คํ๋ง MVC์์ ์ปจํธ๋กค๋ฌ ์ญํ ์ ์ํํฉ๋๋ค. ์ปจํธ๋กค๋ฌ ํด๋์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ฌ๋ฌ ๊ฐ์ ๋ฉ์๋๋ฅผ ํฌํจํ ์ ์์ต๋๋ค.
@Controller
@RequestMapping("/appointments")
public class AppointmentsController
{
// an HTTP POST for /appointments
@RequestMapping(method = RequestMethod.POST)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
// an HTTP GET for /appointments/new
@RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
return new AppointmentForm();
}
}
์์ธ ์ฝ๋ ์ค๋ช :
์์ ์ฝ๋๋ Spring MVC์์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ํ๋ ์์์
๋๋ค.
1. @Controller: ์ด ์ด๋
ธํ
์ด์
์ ํด๋น ํด๋์ค๊ฐ ์ปจํธ๋กค๋ฌ์์ ๋ํ๋
๋๋ค. Spring์ด ์ด ํด๋์ค๋ฅผ ์ปดํฌ๋ํธ ์ค์บํ์ฌ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ , ์์ฒญ ๋งคํ์ ์ฒ๋ฆฌํ ์ค๋น๋ฅผ ํฉ๋๋ค.
2. @RequestMapping("/appointments"): ํด๋์ค ์์ค์์์ ์์ฒญ ๋งคํ์
๋๋ค. ์ด ํด๋์ค์ ๋ชจ๋ ํธ๋ค๋ฌ ๋ฉ์๋์ ๋ํ ์์ฒญ URL์ ์ ๋์ฌ๋ฅผ ์ง์ ํฉ๋๋ค. ์ฆ, ์ด ํด๋์ค ๋ด์ ๋ชจ๋ ๋ฉ์๋์ ๋ํ ์์ฒญ์ "/appointments"๋ก ์์ํด์ผ ํฉ๋๋ค.
3. @RequestMapping(method = RequestMethod.POST): HTTP POST ๋ฉ์๋์ ๋ํ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ์ง์ ํฉ๋๋ค. "/appointments"๋ก ๋ค์ด์ค๋ POST ์์ฒญ์ ์ด ๋ฉ์๋์์ ์ฒ๋ฆฌ๋ฉ๋๋ค.
4. public Map<String, Appointment> get() { ... }: POST ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์
๋๋ค. appointmentBook์์ ์ค๋์ ์์ฝ์ ๊ฐ์ ธ์์ Map์ผ๋ก ๋ฐํํฉ๋๋ค.
5. @RequestMapping(value="/new", method = RequestMethod.GET): "/appointments/new" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ HTTP GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ์ง์ ํฉ๋๋ค.
6. public AppointmentForm getNewForm() { ... }: GET ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์
๋๋ค. ์๋ก์ด ์์ฝ์ ์ํ ํผ์ ์์ฑํ์ฌ ๋ฐํํฉ๋๋ค.
์ด๋ ๊ฒ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ํ๋ฉด Spring MVC๋ ์์ฒญ์ ๋ฐ๋ผ ์ ์ ํ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ฐํํฉ๋๋ค.
@Controller(Mapping Requests : Handler)
Handler : Class๋ฅผ ์ ์ธํ๊ณ ์ฌ์ฉํ๊ธฐ์ ์ฝ๋์ ์์ ์ค์ผ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
ํธ๋ค๋ฌ๋ ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ ๋ก์ง์ ์ ์ํ๋ ๋ฉ์๋์
๋๋ค. ์ปจํธ๋กค๋ฌ ํด๋์ค ๋ด๋ถ์ ๊ฐ ๋ฉ์๋๋ ํน์ URL ๊ฒฝ๋ก์ HTTP ์์ฒญ ๋ฉ์๋(์: GET, POST)์ ๋งคํ๋์ด ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํธ๋ค๋ฌ ์ญํ ์ ํฉ๋๋ค. ํธ๋ค๋ฌ๋ @RequestMapping ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ํน์ URL ๊ฒฝ๋ก์ ์์ฒญ ๋ฉ์๋์ ๋งคํ๋ฉ๋๋ค.
@Controller
public class EmployeeController
{
@RequestMapping("/employee-management/employees")
public String getAllEmployees(Model model)
{
//application code
return "employeesList";
}
@RequestMapping("/employee-management/employees/add")
public String addEmployee(EmployeeVO employee)
{
//application code
return "employeesDetail";
}
}
์์ธ ์ฝ๋ ์ค๋ช :
์์ ์ฝ๋๋ Spring MVC์์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ์ธ EmployeeController ํด๋์ค๋ฅผ ๋ณด์ฌ์ค๋๋ค.
1. @Controller: ์ด ์ด๋
ธํ
์ด์
์ ํด๋น ํด๋์ค๊ฐ ์ปจํธ๋กค๋ฌ์์ ๋ํ๋
๋๋ค. Spring์ด ์ด ํด๋์ค๋ฅผ ์ปดํฌ๋ํธ ์ค์บํ์ฌ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ , ์์ฒญ ๋งคํ์ ์ฒ๋ฆฌํ ์ค๋น๋ฅผ ํฉ๋๋ค.
2. @RequestMapping("/employee-management/employees"): "/employee-management/employees" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์ธ getAllEmployees()๋ฅผ ์ง์ ํฉ๋๋ค.
3. public String getAllEmployees(Model model) { ... }: "/employee-management/employees" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์
๋๋ค. Model ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์์์, ์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ทฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํฉ๋๋ค. ๋ฉ์๋ ๋ด๋ถ์์๋ ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง์ ์ํํ ํ, "employeesList" ๋ทฐ๋ฅผ ๋ฐํํฉ๋๋ค.
4. @RequestMapping("/employee-management/employees/add"): "/employee-management/employees/add" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์ธ addEmployee()๋ฅผ ์ง์ ํฉ๋๋ค.
5. public String addEmployee(EmployeeVO employee) { ... }: "/employee-management/employees/add" ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋์
๋๋ค. EmployeeVO ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์์์, ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ง์์ ์ถ๊ฐํ๋ ๋ฑ์ ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง์ ์ํํฉ๋๋ค. ๊ทธ ํ, "employeesDetail" ๋ทฐ๋ฅผ ๋ฐํํฉ๋๋ค.
์ด๋ ๊ฒ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ์ํ๋ฉด Spring MVC๋ ์์ฒญ์ ๋ฐ๋ผ ์ ์ ํ ํธ๋ค๋ฌ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ฐํํฉ๋๋ค.
์์ ์ฝ๋์์ ์ฒซ ๋ฒ์งธ ํด๋์ค AppointmentsController์ ๋ ๋ฒ์งธ ํด๋์ค EmployeeController๋ ๋ชจ๋ ์คํ๋ง MVC์ ์ปจํธ๋กค๋ฌ์
๋๋ค. ๊ทธ๋ฌ๋ ๋ ํด๋์ค ๊ฐ์๋ ๋ช ๊ฐ์ง ์ฐจ์ด๊ฐ ์์ต๋๋ค.
URL ๋งคํ:
AppointmentsController(class) : @RequestMapping("/appointments")๋ก ์ง์ ๋์ด ์์ด, ๋ชจ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ /appointments ๊ฒฝ๋ก ์๋์์ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค.
EmployeeController(handler) : @RequestMapping("/employee-management/employees")๋ก ์ง์ ๋์ด ์์ด, ๋ชจ๋ ํธ๋ค๋ฌ ๋ฉ์๋๋ /employee-management/employees ๊ฒฝ๋ก ์๋์์ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค.
HTTP ์์ฒญ ๋ฉ์๋:
AppointmentsController(class) : ์ฒซ ๋ฒ์งธ ํธ๋ค๋ฌ ๋ฉ์๋(get())๋ HTTP POST ๋ฉ์๋์ ๋งคํ๋์ด ์์ผ๋ฉฐ, ๋ ๋ฒ์งธ ํธ๋ค๋ฌ ๋ฉ์๋(getNewForm())๋ HTTP GET ๋ฉ์๋์ ๋งคํ๋์ด ์์ต๋๋ค.
EmployeeController(handler) : ๋ ๊ฐ์ ํธ๋ค๋ฌ ๋ฉ์๋ ๋ชจ๋ ๋ช
์์ ์ผ๋ก HTTP ์์ฒญ ๋ฉ์๋๋ฅผ ์ง์ ํ์ง ์์์ผ๋ฏ๋ก, ๊ธฐ๋ณธ์ ์ผ๋ก GET ์์ฒญ์ ๋งคํ๋ฉ๋๋ค.
๋ฆฌํด ํ์
:
AppointmentsController(class) : ์ฒซ ๋ฒ์งธ ํธ๋ค๋ฌ ๋ฉ์๋(get())๋ Map<String, Appointment>์ ๋ฐํํ๊ณ , ๋ ๋ฒ์งธ ํธ๋ค๋ฌ ๋ฉ์๋(getNewForm())๋ AppointmentForm ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
EmployeeController(handler) : ๋ ๊ฐ์ ํธ๋ค๋ฌ ๋ฉ์๋ ๋ชจ๋ String์ ๋ฐํํฉ๋๋ค. ์ฒซ ๋ฒ์งธ ๋ฉ์๋๋ employeesList ๋ฌธ์์ด์ ๋ฐํํ๊ณ , ๋ ๋ฒ์งธ ๋ฉ์๋๋ employeesDetail ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค.
์ด๋ฌํ ์ฐจ์ด์ ๋ค์ ๊ฐ ์ปจํธ๋กค๋ฌ๊ฐ ์๋ก ๋ค๋ฅธ ๊ฒฝ๋ก์ ์์ฒญ ์ ํ์ ๋ํ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ๊ฐ ํธ๋ค๋ฌ ๋ฉ์๋๊ฐ ๋ค๋ฅธ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ ๋ํ๋
๋๋ค.
@Controller Example(์ถ๊ฐ์ ์ค๋ช )
Add attributes to the model
@RequestParam์ Spring MVC์์ HTTP ์์ฒญ์ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉํ ๋ ์ฌ์ฉ๋๋ ์ด๋
ธํ
์ด์
์
๋๋ค. ์ด ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ฉด HTTP ์์ฒญ์ ์ฟผ๋ฆฌ ๋ฌธ์์ด์ด๋ POST ์์ฒญ์ ํผ ๋ฐ์ดํฐ๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์ ์์ต๋๋ค.
์๋์ ์ฝ๋๋ฅผ ๋ณด๋ฉด doLogin() ๋ฉ์๋๊ฐ GET ๋ฐฉ์์ "/login" ๊ฒฝ๋ก์ ๋งคํ๋์ด ์์ต๋๋ค. ์ด ๋ฉ์๋๋ @RequestParam ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ username๊ณผ password๋ผ๋ ๋ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๊ณ ์์ต๋๋ค. ํด๋ผ์ด์ธํธ์์ ์์ฒญ์ด ๋ค์ด์ค๋ฉด Spring MVC๋ ํด๋น ์์ฒญ์ ์ฟผ๋ฆฌ ๋ฌธ์์ด์์ username๊ณผ password ํ๋ผ๋ฏธํฐ๋ฅผ ์ฐพ์ ํด๋น ๋งค๊ฐ๋ณ์์ ๋ฐ์ธ๋ฉํฉ๋๋ค.
์ฆ, ์ด ๋ฉ์๋๋ GET ๋ฐฉ์์ผ๋ก "/login" ๊ฒฝ๋ก์ ์์ฒญ์ด ๋ค์ด์ค๋ฉด ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ฌํ username๊ณผ password๋ฅผ ๋ฐ์์์ ์ฒ๋ฆฌํ๋ ์ญํ ์ ํฉ๋๋ค. ์ฒ๋ฆฌ ํ์๋ ์ฑ๊ณต ํ์ด์ง์ ๊ฒฝ๋ก์ธ success๋ฅผ ๋ฐํํฉ๋๋ค.
* View
JSTL & prefix
<?xml version="1.0" encoding="UTF-8" ?>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:forEach var="i" begin="0" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
<c:if test="${fn:length(friends) > 0}" >
<%@include file="welcome.jsp" %>
</c:if>
JSTL(JavaServer Pages Standard Tag Library)์ JSP(JavaServer Pages)์์ ์์ฃผ ์ฌ์ฉ๋๋ ๋ฐ๋ณต, ์กฐ๊ฑด ๋ฑ์ ๋ก์ง์ ๊ฐํธํ๊ฒ ๊ตฌํํ ์ ์๋๋ก ์ ๊ณต๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. JSTL์ XML ํ์์ผ๋ก ์ ์๋์ด ์์ผ๋ฉฐ, JSP ํ์ด์ง์์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ <%@taglib %> ๋๋ ํฐ๋ธ๋ฅผ ์ฌ์ฉํ์ฌ ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ์ธํด์ผ ํฉ๋๋ค.
์ฌ๊ธฐ์ prefix๋ ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์๋ ๊ฐ ํ๊ทธ๋ฅผ ์๋ณํ๊ธฐ ์ํ ์ ๋์ฌ(prefix)๋ฅผ ์๋ฏธํฉ๋๋ค. JSP ํ์ด์ง์์ JSTL ํ๊ทธ๋ฅผ ์ฌ์ฉํ ๋๋ ํด๋น ์ ๋์ฌ์ ํ๊ทธ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ํ๊ทธ๋ฅผ ์ฐธ์กฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, c:forEach๋ JSTL Core ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์๋ ๋ฐ๋ณต ์ฒ๋ฆฌ๋ฅผ ์ํ ํ๊ทธ์ด๋ฉฐ, fn:length๋ JSTL Functions ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์๋ ํจ์๋ฅผ ํธ์ถํ๋ ํ๊ทธ์
๋๋ค.
์๋๋ ์ฃผ์ด์ง ์ฝ๋์์ ์ฌ์ฉ๋ JSTL ํ๊ทธ๋ค์ ์ค๋ช
์
๋๋ค:
1. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>: ์ด ๋๋ ํฐ๋ธ๋ JSTL Core ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๊ฒ์ผ๋ก, c ์ ๋์ฌ๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ๊ทธ๋ค์ ์ฐธ์กฐํ ๊ฒ์์ ์ ์ธํฉ๋๋ค.
2. <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>: ์ด ๋๋ ํฐ๋ธ๋ JSTL Functions ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๊ฒ์ผ๋ก, fn ์ ๋์ฌ๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ์๋ฅผ ์ฐธ์กฐํ ๊ฒ์์ ์ ์ธํฉ๋๋ค.
3. <c:forEach var="i" begin="0" end="5">: ์ด ํ๊ทธ๋ JSTL Core ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ฐ๋ณต ์ฒ๋ฆฌ๋ฅผ ์ํ ํ๊ทธ์
๋๋ค. var ์์ฑ์ ๋ฐ๋ณต๋ฌธ ๋ด์์ ํ์ฌ ํญ๋ชฉ์ ์ฐธ์กฐํ ๋ณ์๋ช
์ ์ง์ ํ๊ณ , begin๊ณผ end ์์ฑ์ ๋ฐ๋ณต ๋ฒ์์ ์์๊ณผ ๋์ ์ง์ ํฉ๋๋ค.
4. <c:if test="${fn:length(friends) > 0}" >: ์ด ํ๊ทธ๋ JSTL Core ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์กฐ๊ฑด ์ฒ๋ฆฌ๋ฅผ ์ํ ํ๊ทธ์
๋๋ค. test ์์ฑ์๋ ์กฐ๊ฑด์ ์ง์ ํ๋ฉฐ, ${fn:length(friends) > 0}๋ JSTL Functions ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ fn:length() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ friends๋ผ๋ ๊ฐ์ฒด์ ๊ธธ์ด๊ฐ 0๋ณด๋ค ํฐ์ง๋ฅผ ํ์ธํ๋ ์กฐ๊ฑด์
๋๋ค.
๋ฐ๋ผ์ JSTL์ ์ฌ์ฉํ๋ฉด์ prefix๋ฅผ ์ค์ ํ์ฌ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ๊ทธ๋ค์ ์ฝ๊ฒ ์ฐธ์กฐํ ์ ์์ผ๋ฉฐ, ๋ฐ๋ณต, ์กฐ๊ฑด ๋ฑ์ ๋ก์ง์ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์์ต๋๋ค.