05-21 07:17
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- literal
- descriptive statistics
- External Scheme
- systemd
- Java
- Operator
- 리눅스 마스터 1급
- 백준
- Inheritance
- Mac
- X.org
- Entity Set
- X윈도우
- selenium
- Physical Scheme
- BFS
- OOP
- Entity
- Class
- Polymolphism
- python
- 셀레니움
- Binary Search
- spring
- preprocessing
- 리눅스
- dbms
- Unity
- 자바
- Reference Type
Archives
- Today
- Total
Byeol Lo
Spring - MVC와 템플릿 엔진 본문
MVC : Model, View, Controller 를 뜻한다. 과거에는 View(보여지는 곳 - ex. html, css 등)와 Controller(url path를 찾아내는 컨트롤러)가 통합되어 있었다. 즉, view 밖에 없었다. 하지만 역할이 backend, frontend로 분리하여 frontend는 디자인을 위한 역할, backend는 데이터관리와 비즈니스 프로세스를 위한 것으로 나누어서 일 효율을 증가시켰다. View는 화면과 관련된 일만, 비즈니스 로직이나 서버 뒷단에 관련된 일은 Controller로 요청을 처리하고, Model에 담아서 다시 건네주는 형식으로 처리하는 패턴을 사용한다.
처음 MVC를 공부할 때 이해했던 경험을 여기에 쓰자면, Annotation으로 @Controller로 해당 객체를 등록하면 객체 안의 메소드들이 Controller안에(?) 저장된다고 생각하고, GetMapping("path")로 url 경로를 지정해주는 것으로 생각하고 넘어갔다. 이런 편이 더 깊게 안파고 들고 상관관계만 파악해서 개발을 한 다음에 나중에 세세히 따져가는 공부를 하기에 좋은듯하다. @Controller의 어노테이션을 안적으면 Spring 컨테이너에서 해당 객체를 무시하게 되고, @GetMapping도 안적으면 path를 찾을 수 없게 된다.
Controller
pacakage ~~
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController(Model model) {
//MVC 방식
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.adddAttribute("name", name);
return "hello-template";
}
// ...
}
View
//src > main > resources > templates > hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset='UTF-8'>
<title>hello, world</title>
</head>
<body>
<p th:text="'hello ' + ${name}"> This is HTML Template </p>
</body>
</html>
html의 p태그 내용은 실제로 다른 분들이 서버 없이 볼때 보기 편하라고 넣는 것이다.
'BackEnd > Spring' 카테고리의 다른 글
Spring IOC(Inversion of Control), DI(Dependency Injection) (0) | 2022.08.30 |
---|---|
Spring - static 폴더 (0) | 2022.08.24 |
build.gradlew (0) | 2022.08.24 |
Spring 시작하기 (0) | 2022.08.21 |
Comments