스프링 웹 개발 기초
<!DOCTYPE HTML>
<html>
<head>
<title>Hello Static</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠입니다.
</body>
</html>

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")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
@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>
<!--위의 코드에서 hello! empty는 단순하게 html 파일의 경로를 복사하여 실행하면 볼 수 있는 것이고
'hello ' + ${name}는 템플릿 엔진으로서 작동을 하면 볼 수 있는 것
따라서 hello! empty 위치에 있는 내용은 서버 없이 html의 내용만 볼 때 사용됨-->
</body>
</html>

@GetMapping
@ResponseBody//이 부분이 의미하는 것은 html의 body 태그가 아니라
//http 통신 프로토콜이 head부와 body부가 있는데 이 body부에 아래의 return 값을 직접 넣어주겠다는 의미
public String helloString(@RequestParam("name") String name) {
return "hello" + name;//name 값을 Spring으로 하게 되면 hello Spring이 그대로 전달됨
}//템플릿 엔진과의 차이점은 이 경우에 페이지의 소스를 봤을 때 html 코드가 전혀 없고 hello Spring이라는 데이터가
//그대로 전달됨을 알 수 있음
@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;
}
//command + n 눌러서 getter and setter 클릭하면 코드 자동완성해줌
}

