welcome Page(정적 컨텐츠)
resources/static/index.html
<!DOCTYPE HTML> <html> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> Hello <a href="/hello">hello</a> </body> </html>
Controller
Java/Controller/HelloController.Class
@Controller public class HelloController { @GetMapping("hello") public String hello(Model model) { model.addAttribute("data", "hello!!"); return "hello"; } }
Annotation을 쓰는것으로 Spring FrameWork가 해당 클래스가 어떤 역할인지 정해준다. 지금 같은 상황에서는 HelloController 클래스가 컨트롤러의 역할이라는 것을 알려준다.
웹브라우저에서 localhost:8080/A가 입력되었을때
Spring-Boot에 내장된 톰켓서버를 거쳐서 Controller로 가고 GetMapping에 A라는 URL과 일치하면 작동하는 방식이다.
model.addAttribute("data", "hello!!")에 data라는 Key값에 hello!!라는 Value값을 지정해준뒤 return값으로 문자를 반환하면 return "B"를 통해 viewResolver가 templates/B.html 화면을 찾아 처리한다.
resources/templates/hello.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'안녕하세요. ' + ${data}" ></p> </body> </html>
2번 line에 xmlns:th="http://www.thymeleaf.org"은 템플릿 엔진으로 Thymeleaf을 사용하기 위해 작성한 것이다.
8번 line에 <th:text="'안녕하세요. ' + ${data}" ></>에서 ${data}는 Controller에서 Key값인 data에 지정된 Value값으로 치환한다.
resources/static/hello-static.html
<!DOCTYPE HTML> <html> <head> <title>static content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> 정적 컨텐츠 입니다. </body> </html>
hello-static과 Mapping된 Controller가 없을 경우 바로
서버를 사용하지 않고 파일 그대로 웹브라우저로 전달한다.
URL도 localhost8080/hello-static.html로 표기된다.
Model View Controller
Controller
Java/Controller/HelloController.Class
@Controller public class HelloController { @GetMapping("hello-mvc") public String helloMvc(@RequestParam("name") String name, Model model) { model.addAttribute("name", name); return "hello-template"; } }
name이라는 파라미터에 String type인 name을 지정해준다.
model.addAttribute("name", name); 을 보면 모델에 파라미터 "name"값을 string name을 저장하고 viewResolver가 return값으로 반환되는 화면에 처리한다.
url = {localhost:8080/hello-mvc?name=A}입력시 모델의 name에 A를 저장
-> model(name:A)
View
resources/templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org"> <body> <p th:text="'hello ' + ${name}"></p> </body> </html>
Controller에서 모델의 name에 저장된 name값으로 치환되어 화면에 출력된다.
@ResponseBody 문자 반환
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
}
viewReslover를 사용하지 않고 HTTP의 BODY에 문자 내용을 직접 반환
(HTML BODY TAG를 말하는 것이 아님)
URL = {localhost:8080/hello-string?name=A} 입력시
hello A 만 출력된다.
@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;
}
static class Hello {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
}
@ResponseBody 를 사용하고, 객체를 반환하면 객체가 JSON으로 변환됨
URL = {localhost:8080/hello-api?name=A} 입력시
{"name" : "A"}로 객체가 출력된다.