Light Blue Pointer
본문 바로가기
Developing/TIL(Develop)

정적페이지와 동적페이지

by Greedy 2023. 11. 3.

정적페이지와 동적페이지

정적 페이지 

static에 html넣고 호출해보기

resources→static→new→HTML→hello.html 만듦

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello Spring</title>
</head>
<body>
Hello, Spring 정적 웹 페이지!! (static)
</body>
</html>  

main→java→com.~~→html→HtmlController 만듦

package com.sparta.springmvc.html;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HtmlController {

    private static long visitCount = 0;

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

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

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

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

이번에는 String이 아니라 HTML을 반환해서 RequestBody가 안 붙는다!

@Controller가 붙은 클래스의 Method에서 반환 타입이 String이면 반환하는 String에 부합하는 페이지를 찾아서 반환해 준다고 함

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

http://localhost:8080/static/hello

를 브라우저에 쳐봄 → 에러남

해결방법 1 :

http://localhost:8080/hello.html

를 직접 입력하면 아까 만든 hello.html페이지가 뜸

hello.html은 이미 완성된 상태이기 때문에 Controller를 한번 거쳐서 반환할 필요가 없다고 함

 

해결방법 2 :

기본적으로 thymeleaf에서는 templates 폴더 안에서 html을 찾는대

thymeleaf 주석처리하니까 /static-hello로 불러짐

dependencies {
    //implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

http://localhost:8080/static-hello

정적 html은 Controller를 거쳐서는 반환할 수 없을까?

의미는 없지만 가능하긴 하대

thymeleaf : 동적인 html파일을 만들기 위한 library라고 함

⭐thymeleaf : https://www.thymeleaf.org/

Thymeleaf is a modern server-side Java template engine for both web and standalone environments.

 

해결방법 3 :

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

재호출한다

 

그냥 hello.html불렀을때)

 

/html/redirect로 hello.html불렀을때)

Location에 hello.html로 요청이 다시 한번 되는거래

html주소가 바뀌었을때 redirect로 구현해놓으면 코드를 한번만 바꾸면 된다 함

아니면 사용자에게 html이름을 알려주기 싫을수도 있고

redirect가 사용될때가 있다고 함

 

templates에 html넣고 호출해보기

resources→templates→new→HTML→hello.html 만듦

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Hello Spring</title>
</head>
<body>
Hello, Spring 정적 웹 페이지!! (templates)
</body>
</html>
@GetMapping("/html/templates")
    public String htmlTemplates() {
        return "hello";
    }

thymeleaf 엔진이 hello에 html이 안 붙어도 templates에 있는 hello.html잘 찾아다줌

@Controller annotation이 달린 클래스에서 templates에 있는 html을 불러오고 싶으면 이름 String만 return하면 된다고 함

동적 페이지

private static long visitCount = 0;

HtmlController 클래스에 저 필드 추가함

@GetMapping("/html/dynamic")
    public String htmlDynamic(Model model) {//Front Controller에서 Model 객체를 넣어주면 이용
        visitCount++;//요청이 들어오면 visitCount를 1 증가시킴
        model.addAttribute("visits", visitCount);//모델에 변동이 필요한 데이터를 넣어줌
        return "hello-visit";
    }

데이터를 Model에 넣는데 Spring에서 Model 객체를 제공

⭐model.addAttribute()

⭐public String htmlDynamic(Model model)

resources→templates→hello-visit.html 만들어줌

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Hello Spring</title></head>
<body>
<div>
    Hello, Spring 동적 웹 페이지!!
</div>
<div>
    (방문자 수: <span th:text="${visits}"></span>)//아까 model에 넣어준 attribute의 이름
</div>
</body>
</html>

 

새로고침하니까 2됨

static 변수라서 그렇다고 함

 

'Developing > TIL(Develop)' 카테고리의 다른 글

Jackson이란 무엇일까  (0) 2023.11.06
데이터를 클라이언트에게 반환하는 방법  (0) 2023.11.06
Controller이해하기  (0) 2023.11.02
Spring MVC  (0) 2023.11.02
Lombok과 application.properties  (0) 2023.11.02