인프런 강의 < 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 > 정리
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>
1. 웹 브라우저에서 http://localhost:8080/hello-static.html
검색
2. Tomcat
에서 요청을 받음
3. controller
가 존재하지 않는 것을 판단
4. resources
내에 있는 hello-static.html을 찾고 존재하면 반환
Controller
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
View
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
Tomcat
에서는 hello-mvc에 Mapping되어 있는 Controller 존재를 찾음@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
}
@ResponsBody
는 HTTP의 body에 직접 데이터를 내려주는 것을 말한다
페이지 소스보기로
로 확인해보면 html
형식이 아님@Controller
public class HelloController {
@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;
}
}
}
json
형식(key, value 구조
)으로 나옴{"name":"spring!!"}
< 자료 출처: 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 >