static/index.html을 올려두면 page 기능을 제공한다. (정적페이지)




페이지 소스를 보면 data가 hello!!로 변경되어 있는 것을 볼 수 있다.

resources:templates/ + {viewName} + .htmlhttp://localhost:8080/hello-static.html로 실행
Controller
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String hello(@RequestParam("name") String name, Model model) { // 외부에서 파라미터를 받음
model.addAttribute("name", name);
return "hello-template";
}
}
View
templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
Hello
<p th:text="'hello ' + ${name}" >hello! empty</p>
</body>
</html>
파라미터는 ?key=파라미터으로 넣어준다.
실행 : localhost:8080/hello-mvc?name=value


@GetMapping("hello-string")
@ResponseBody // Body부분에 넣을 것것
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
HTML을 사용하지않고 그대로 데이터가 나타난다.

페이지 소스 보기 화면

@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형식으로 데이터를 보내게 된다.

