By default, Spring Boot serves static content from a directory called /static
in the classpath or from the root of the ServletContext.
기본적으로 static폴더 안에 위치한다
통신 개요
resources:static/
에서 hello-static.html파일을 찾아서 올려준다예전에는 3분류로 나뉘어져있지않고 view에 대부분의 기능을 넣어놨다
View : 화면을 그리는것을 담당
Controller, Model : 로직등 기술적인 것을 담당
형태
@GetMapping("hello-mvc") //url을 hello-mvc만 입력하면 오류가 발생한다
//MissingServletRequestParameterException 발생
//아래 @RequestParam을 통해 선언된 name에 인자를 넘겨 줘야 한다
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name", name);
return "hello-template";
}
<!--위에서 name이 url에서 넘겨받은 값으로 변경되어서 아래 name에 치환되어 들어간다-->
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
name="내용"
부분이 없다면 오류가 발생하게 된다View와 같은 파일을 따로 조작하지 않고 주어진 입력 그대로를 화면에 출력해준다
@GetMapping("hello-api")
@ResponseBody //html의 body가 아닌 http에서의 body부분에 내가 직접 아래 내용을 넣겠다는 의미
public String helloApi(@RequestParam("name") String name) {
return "hello " + name;
}
구현된 모습
@GetMapping("hello-api")
@ResponseBody //동일하게 url에서 값을 받지만 이를 key-value형태의 json데이터로 갖게 된다
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello; <-- 객체를 json형태로 반환한다 (기본설정)
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JsonConverter와 StringConverter가 대기하다가 객체냐 문자냐에 따라서 맞는 컨버터로 동작한다
@ResponseBody사용
기본 문자 처리 : StringHttpMessageConverter
기본 객체 처리 : MappingJackson2HttpMessageConverter