[김영한님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술] 스프링 웹 개발 기초 - 'MVC와 템플릿 엔진' 수강
This commit is contained in:
@@ -7,4 +7,5 @@
|
||||
- [빌드하고 실행하기](https://github.com/banjjoknim/spring-introduction/blob/master/Lectures/Lecture04.md)
|
||||
|
||||
## 스프링 웹 개발 기초
|
||||
- [정적 컨텐츠](https://github.com/banjjoknim/spring-introduction/blob/master/Lectures/Lecture05.md)
|
||||
- [정적 컨텐츠](https://github.com/banjjoknim/spring-introduction/blob/master/Lectures/Lecture05.md)
|
||||
- [MVC와 템플릿 엔진](https://github.com/banjjoknim/spring-introduction/blob/master/Lectures/Lecture06.md)
|
||||
38
SpringBoot-Introduction/Lectures/Lecture06.md
Normal file
38
SpringBoot-Introduction/Lectures/Lecture06.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# MVC와 템플릿 엔진
|
||||
- `MVC` : `Model`, `View`, `Controller`
|
||||
- 과거에는 `View`에서 모든 것을 다 했다(`Model 1 방식`).
|
||||
- 개발을 할 때는 관심사를 분리해야 한다. 역할과 책임.
|
||||
- `View`는 화면을 그리는데 모든 역량을 집중해야 한다.
|
||||
- `Model`, `Controller`과 관련된 부분들은 비즈니스 로직과 관련이 있거나 내부적인 것들을 처리하는데 집중해야 한다.
|
||||
- `View`는 화면에 관계된 일만.
|
||||
- 비즈니스 로직과 서버 뒷단에 관련된 것은 `Controller`나 뒷단 비즈니스 로직에서 처리하고 `Model`에 화면에 필요한 것들을 담아서 화면쪽에 넘겨준다.
|
||||
|
||||
---
|
||||
|
||||
## @RequestParam
|
||||
- 외부에서 파라미터를 받을 때 사용한다.
|
||||
|
||||
---
|
||||
|
||||
## Thymeleaf
|
||||
- `html`을 그대로 쓰고 그 파일을 서버 없이 바로 열어봐도 껍데기를 볼 수 있다는 장점이 있다.
|
||||
- 다음 `html` 코드는 템플릿 엔진으로 동작을 하면 내용(`hello! empty`)이 서버에서 전달해준 데이터로 치환(`'hello ' + ${name}`)된다.
|
||||
- `<p th:text="'hello ' + ${name}">hello! empty</p>`
|
||||
|
||||
---
|
||||
|
||||
## 인텔리제이 옵션보기
|
||||
- 원하는 부분을 선택한 뒤 `ctrl + p`를 누르면 파라미터 정보 옵션을 볼 수 있다.
|
||||
- `@RequestParam`의 `required` 옵션의 기본값은 `true`이기 때문에 파라미터를 무조건 전달해줘야 한다(`false`로 지정하면 값을 넘기지 않아도 된다).
|
||||
|
||||
---
|
||||
|
||||
## MVC, 템플릿 엔진 이미지
|
||||

|
||||
- 웹 브라우저에서 `localhost:8080/hello-mvc`로 접근하면 내장 톰캣 서버가 스프링에게 요청을 던진다.
|
||||
- 스프링은 `helloController`에 해당 url에 매핑되어 있는 메서드를 호출해준다.
|
||||
- 그리고 리턴을 해줄때 `hello-template`와 `model(name:spring)`을 스프링한테 리턴해준다.
|
||||
- 스프링의 `viewResolver`가 뷰를 찾고 템플릿 엔진을 연결시켜준다. templates/`hello-template`.html(메서드가 리턴한 값인 `hello-template`)를 찾아서 `Thymeleaf` 템플릿 엔진한테 처리해달라고 넘긴다.
|
||||
- `Thymeleaf` 템플릿 엔진이 랜더링해서 변환한 `html`을 웹 브라우저에 반환한다.
|
||||
|
||||
---
|
||||
BIN
SpringBoot-Introduction/Lectures/images/MVC,-템플릿-엔진-이미지.PNG
Normal file
BIN
SpringBoot-Introduction/Lectures/images/MVC,-템플릿-엔진-이미지.PNG
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
@@ -3,6 +3,7 @@ package hello.springintroduction.controller;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
public class HelloController {
|
||||
@@ -12,4 +13,10 @@ public class HelloController {
|
||||
model.addAttribute("data", "spring!!");
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@GetMapping("hello-mvc")
|
||||
public String helloMvc(@RequestParam("name") String name, Model model) {
|
||||
model.addAttribute("name", name);
|
||||
return "hello-template";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
<p th:text="'hello ' + ${name}">hello! empty</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user