# Model

## Model이란

<mark style="color:blue;">`Model`</mark>은 <mark style="color:blue;">`MVC Pattern`</mark>의 M에 해당하는 것으로 **Controller와 View(JSP) 사이에 전달되는 데이터**를 의미한다. Spring에서는 Model 클래스를 통해 처리하도록 구현되어 있다.

## Controller & View 생성

<mark style="color:blue;">`boot02controller`</mark> 프로젝트에 Mapping과 JSP View를 하나씩 추가한다.

### Controller mapping 생성

<mark style="color:blue;">`DemoController`</mark>에 다음과 같이 코드를 추가한다.

```java
@RequestMapping("/modelTest")
public String modelTest(Model model) {
	model.addAttribute("testString", "Hello Spring Boot");
	model.addAttribute("testNumber", 12321);
	return "modelTest";
}
```

매개변수에 Model을 선언하면 요청 발생 시 Spring에서 자동으로 해당 객체를 생성하여 사용할 수 있도록 주입한다. 따라서 Model 객체에 <mark style="color:red;">`(이름, 값)`</mark>을 추가하면 View에서 이름으로 값을 호출하여 사용할 수 있다. 이름은 <mark style="color:red;">`String`</mark>으로 설정해야 하며, 값은 <mark style="color:red;">`Object`</mark> 형태 즉, 아무 형태나 설정할 수 있다.

<div align="left"><img src="/files/EhKZYcN280sVgrvEgsIF" alt=""></div>

### JSP View 생성

Controller mapping에서 반환한 주소인 <mark style="color:red;">`modelTest`</mark>에 맞는 <mark style="color:blue;">`JSP View`</mark>를 생성한다.

{% hint style="info" %}
주의&#x20;

ViewResolver가 적용되어 있으므로 Prefix와 Suffix를 잊지 말아야 한다.

* prefix - <mark style="color:red;">`/WEB-INF/views/`</mark>
* suffix - <mark style="color:red;">`.jsp`</mark>

[JSP 연동](/web/back-end/spring-boot/spring-controller/add-jsp.md#view-resolver) 참조
{% endhint %}

{% code title="modelTest.jsp" lineNumbers="true" %}

```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>testString : ${testString}</h1>
<h1>testNumber : ${testNumber}</h1>
```

{% endcode %}

Model을 출력할 때는 EL을 사용하며 다음 값은 동일한 결과를 가진다.

* ${testString}
* ${requestScope.testString}

<div align="left"><img src="/files/yvp6SZdcDiu6u7JicI4L" alt=""></div>

## 결과 확인

브라우저에서 다음 주소를 입력하여 결과를 확인한다

{% embed url="<http://localhost:8888/boot02/modelTest>" %}

<div align="left"><img src="/files/FgbrfGzAj5URBSUvIVPv" alt=""></div>

## 결론

<mark style="color:blue;">`Model`</mark>은 <mark style="color:blue;">`MVC Pattern`</mark>에서 프로그래밍 처리를 하는 <mark style="color:blue;">`Controller`</mark>의 결과를 완성될 화면인 <mark style="color:blue;">`View`</mark>에 전달하는 역할을 한다. <mark style="color:blue;">`View`</mark>는 여러 종류가 있을 수 있으며 <mark style="color:blue;">`JSP`</mark>도 그 중 하나이다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sysout.co.kr/web/back-end/spring-boot/spring-controller/model.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
