[Spring] 정적페이지, 동적페이지 반환하기

yoon·2024년 1월 20일

spring-boot

목록 보기
11/41
post-thumbnail

✅ 정적페이지(static 폴더)

✔ 1. 직접 접근

url로 바로 호출.
http://localhost:8080/hello.html

✔ 2. controller 거치기

리턴값으로 파일경로 입력한다.

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

✔ 3. redirect

리턴값으로 redirect:/ 작성 후 직접 접근 경로 입력

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

✔ 4. templates 폴더안에 있을 때

html파일 이름만 입력해주면 된다.

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

✅ 동적페이지(templates 폴더)

✔ View에 Model을 적용 → 동적 웹페이지 생성

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

페이지에 값 전달하기

<div>
    (방문자 수: <span th:text="${visits}"></span>)
</div>
profile
하루하루 차근차근🌱

0개의 댓글