[TIL] 230626 Spring: 정적페이지와 동적페이지

CountryGirl·2023년 6월 26일

TIL

목록 보기
9/80

📌 Controller

✅ Static

tymeleaf 엔진 O

Controller는 기본적으로 resouces > templates 에서 .html 을 찾는다.

@GetMapping("/static-hello")
    public String hello() {
        return "hello.html";
    }

templates 에는 hello.html 이 없다!
오류 발생: http://localhost:8080/hello/static-hello

만약 resouces > static 에 있는 .html 을 찾기 위해선
Controller 를 통해서 접근하지 않고, html 로 바로 접근한다!!
http://localhost:8080/hello.html

tymeleaf 엔진 X

http://localhost:8080/hello/static-hello
Controller를 통해 접근이 가능하다.

하지만 이미 만들어진, 변하지 않는 정적 페이지를 굳이 Controller를 통해 접근해야할까?
그냥 바로 이름.html 로 접근하는 것이 좋다!

✅ templates (tymeleaf 엔진 O)

@GetMapping("/html/templates")
    public String htmlTemplates() {
        return "hello";
    }

hello.html 이지만 .html 를 생략하고 바로 html의 이름을 return 하면 된다.(return 타입이 String)


📌 @ResponseBody

@ResponseBody (X)

return 타입이 String 일 때, 리턴을 하면 templates 에서 html을 찾는다.

@GetMapping("/hello")
    public String hello() {
        return "hello";
    }

http://localhost:8080/hello 하게 되면 hello.htmlresource > templates 에서 찾아서 hello.html 을 보여준다.

@ResponseBody (O)

return 타입이 String 일 때, String 문자열을 출력한다.

@GetMapping("/hello")
@ResponseBody
    public String hello() {
        return "String hello 나는 문자열이야";
    }

http://localhost:8080/hello 을 하게 되면 hello 문자열을 보여준다.

profile
💻🌾시골소녀의 엉망징창 개발 성장일지🌾💻 (2023.05.23 ~)

0개의 댓글