스프링 웹개발 기초
- 웹개발 방법 3가지
- 정적 컨텐츠 - 서버에서 아무것도 안하고 파일 그대로 웹 브라우저에 전달
- MVC와 템플릿 엔진 - 서버에서 html파일로 변형해서 웹 브라우저에 전달
- API - JSON 데이터 포맷으로 웹 브라우저 또는 클라이언트에 전달, 서버끼리 통신할 때도 사용
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset = UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>
<정적 컨텐츠 동작 과정>
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name", name);
return "hello-template";
}
}
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
<MVC와 템플릿 엔진 동작 과정>
@ResponseBody
: html이 아닌 http의 통신 프로토콜이 header와 body로 나눠지는데,@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello " + name; // 문자열 리턴
}
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello; // 객체 리턴
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
<API 동작 과정>