롤 아이템 스태틱이 아니다.
정적인 이라는 뜻을 가지고 있는데 말 그대로 html 코드를 그대로 넘겨주는 경우이다.
이는 html코드를 그대로 웹 브라우저에 보여주는 방식이라 어떤 프로그램을 돌리거나 할 수는 없다.
내장톰켓 서버는
hello-static 관련 controller를 찾는다
만약 있다면 controller에 따라 작동하지만, 없다면
resource/static안에서 hello-static.html찾아서 반환한다.
나중에 그림 추가
view는 보여지는 디자인 영역에 모든것을 집중해야한다.
bussiness logic이 있거나 서버쪽에 집중되어있다.
따라서 MVC 패턴을 통해 분리해서 작성을 해야 유지보수가 편한다.
내장톰켓 서버는
controller를 찾는다. mapping이 되어있으니깐 method를 호출해주고, 이를 스프링에 넘겨준다.
viewresolver가 return과 같은 name을 가진 html을 찾아서 이를 변환해서 반환해준다.
나중에 그림 추가
@ResponseBody라는 API를 호출해서 사용하기로 한다.
이를 하는 방법이 두개정도가 있는데
@ResponseBody을 호출하고 string을 return 해주는 방법
@ResponseBody를 호출하고 class를 만들고 객체를 반환해주는 방법
내장톰켓 서버는
2-1. httpMessageConvertor 가 동작, StringConverter가 작동하고 문자를 그대로 반환하고 끝
StringMessageConvertor
2-2. 만약 객체가 들어오면 이 객체를 보고
httpMessageConvertor 가 동작, JsonConverter가 작동하고 이를 반환해준다.
MappingJackson2HtppMessageConverter
Jackson2는 Json 컨버터 라이브러리이다.
나중에 그림 추가
지금까지 적힌걸 전부 작성한 컨트롤러는 다음과 같다
package com.example.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.RequestAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("Hello")
public String hello(Model model) {
model.addAttribute("data", "This is test value ");
return "hello";
}
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
@GetMapping("hello-string")
@ResponseBody
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;
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}