정적컨텐츠
- 파일을 그대로 웹 브라우저에 내려주는것
- 스프링부트는 정적컨텐츠 기능을 자동으로 제공한다
MVC (Model View Controller) 와 템플릿 엔진
- 현재 가장 많이 개발하는 방식 (예 : JSP, PHP 등)
- HTML을 그냥 주는게 아니라 server에서 프로그래밍 후 동적으로 바꿔서 내려주는것
- 이걸 위해 controller, Model, 템플릿 엔진 사용한 화면(View) 을 사용해서 MVC라 함
- 과거에는 Controller와 View가 분리되어 있지 않고 View에서 모든 개발을 다 했다. 이걸 Model1 방식 이라고 한다
- View는 화면을 그리는데 모든 역량을 집중해야하고 Controller와 Model은 비지니스 로직과 관련이 있거나 내부적인걸 처리하는것에 집중해야한다.
- Model1 방식으로 하면 JSP 파일 하나에 코드가 수천 Line이 넘어간다. View 파일 하나안에 DB도 접근하고 Controller 로직도 다있고....
- 그래서 Model, View, Controller 로 쪼갠것. 현재 개발 방식은 View와 Controller를 쪼개는것이 기본이다 → MVC 패턴
controller
package hello.hellospring.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;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("/hello-MVC")
public String helloMvc(@RequestParam String name, Model model)
{
model.addAttribute("name", name);
return "/hello-template";
}
}
hello-template.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'hello. ' + ${name}" ></p>
</body>
</html>
API
- JSON Data 구조 Format으로 client한테 Data 전달하는 방식
- vue.js 나 React 등에 데이터 내려주면 화면은 client(프론트 개발자) 가 그려주는것
- server끼리 통신할때 사용 (server 끼리 통신할땐 HTML 필요 없으니까)
@ResponseBody
HTTP 응답 Body에 직접 Data를 넣어주겠단것. 이 문자가 요청한 client에게 그대로 내려감. html 태그가 아닌 문자 Data 그대로 전달됨
얘가 있으면 ViewResolver가 아닌 HttpMessageConverter가 작동해서 웹 브라우저에 Data 응답을 해줌
controller (문자만 client한테 내린 모습)
package hello.hellospring.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;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("/hello-spring")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello"+name;
}
}
controller (문자가 아닌 Data를 client한테 내린 모습)
package hello.hellospring.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;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello;
}
// getter, setter는 JavaBean규약, 객체 프로퍼티 접근 방식, 메서드를 통해서 데이터에 접근하는 방식
static class Hello{
private String name;
// Alt + insert 같이 누르면 getter, setter 단축키
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
json 방식으로 client한테 내려감. Json은 Key, Value로 이뤄진 구조. xml과 json 많이 썼었는데 최근엔 json으로 통일.
xml 방식은 무겁고 코드 열고 닫고 두번 쓰는 반면 Json은 심플. 스프링도 Json으로 반환되는게 기본으로 설정되어있다.
Jackson은 Data를 Json으로 바꿔주는 라이브러리. 스프링은 Jackson을 기본으로 사용하고 있음
'Spring > 스프링 입문 - 스프링 부트, 웹 MVC, DB 접근 기술' 카테고리의 다른 글
7. 회원 도메인과 리포지토리 생성 | 테스트 케이스 작성 (0) | 2023.03.05 |
---|---|
6. 비지니스 요구사항 정리 | 일반적인 웹 애플리케이션 계층 구조 (0) | 2023.03.05 |
4. Server에서 스프링 build하고 실행 | 서버가 구축된 환경이라면 war 아니면 jar 형식 (0) | 2022.11.07 |
3. View 환경설정 | Welcome Page | Controller로 Thymeleaf 템플릿 엔진 동작 확인 (0) | 2022.06.15 |
2. Gradle은 의존관계가 있는 라이브러리를 함께 다운로드한다 | 스프링 부트 라이브러리 | 테스트 라이브러리 (0) | 2022.06.15 |