Spring 6. 정적 페이지 vs 동적 페이지

김창민·2024년 8월 9일

BE

목록 보기
26/50

정적 페이지

말 그대로 멈춰있는 페이지이다. 호출한 당시의 html파일이 그대로 브라우저에 표현되는 것이다.
총 3가지의 방식으로 정적 페이지를 호출 할 수 있다.

1. static 폴더

서버에 html을 바로 요청하면 출력이 된다. 물론 Controller를 거쳐서 html도 반환이 되는데, 굳이 Controller를 거칠 필요가 없는 완성된 파일일 뿐 더러 implementation의 dependencty 수정도 필요하다.

2. Redirect

@GetMapping("/html/redirect")
public String htmlStatic() {
    return "redirect:/hello.html";
}

http://localhost:8080/html/redirect로 접근을 하면 hello명의 html 파일을 반환한다.

3. Template engine에 View 전달

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

templates 폴더에 정적 html파일을 추가하고, html 파일명을 반환하면 정적 페이지를 호출 할 수 있다.
이 방식은 브라우저에서 바로 접근하지 못하게 하면서 특정 상황에 Controller를 통해서 제어가 가능하다.

동적 페이지

정적 페이지와 다르게 변화하는 페이지이다.
1. Client의 요청을 Controller-> Model로 처리
2. Thymeleaf(Template engine)에게 View, Model를 전달합니다.
3. Thymeleaf는 View에 Model을 적용
4. Client에게 View 전달

View

//hello-visit.html
<div>
  (방문자 수: <span th:text="${visits}"></span>)
</div>

Model

private static long visitCount = 0;
@GetMapping("/html/dynamic")
    public String htmlDynamic(Model model) {
        visitCount++;
        model.addAttribute("visits", visitCount);
        return "hello-visit";
    }

이렇게 작성하면 /html/dynamic가 입력되고, htmlDynamic가 실행이 되면서 visitCount의 값을 1 올리고, Spring Model 객체를 통해 해당 값을 visits라는 이름으로 감싸서 templates에 있는 hello-visit.html파일에 visits라고 지정된 곳으로 값을 던진다.

그러면 이 페이지에 접근 할 때 마다 접근 횟수가 증가한다. (서버가 꺼질때까지)

profile
일일 회고 : https://rlackdals981010.github.io/

0개의 댓글