정적컨텐츠 흐름
resources/hello-static.html
<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
정적컨텐츠입니다.
</body>
</html>
MVC : Model, View, Controller
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;
@Controller
public class HelloController {
@GetMapping("hello") // request hello가 들어올시 선언한 hello메소드 호출해준다.
public String hello(Model model){ // model : mvc
model.addAttribute("data", "hello!!");
return "hello"; // resources/templates/hello.html을 스프링을 찾아서
}
@GetMapping("hello-mvc") // hello-mvc로 요청이 들어온 경우
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name",name);
return "hello-template"; // hello-template로 반환
}
}
View - 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}">hello! empty</p>
</body>
</html>
@ResponseBody 사용시
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") // request hello가 들어올시 선언한 hello메소드 호출해준다.
public String hello(Model model){ // model : mvc
model.addAttribute("data", "hello!!");
return "hello"; // resources/templates/hello.html을 스프링을 찾아서
}
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name",name);
return "hello-template";
}
@GetMapping("hello-string")
@ResponseBody // View가 없이 그대로 데이터를 전달
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; // @ResponseBody - 객체반환 : json으로 반환
}
static class Hello { // static class
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
}
API 요청 완료!