본문 바로가기
Java , Spring/Spring

Spring Boot (2) Welcome page 만들기

by 방배킹 2023. 1. 30.

1. src > resources > static > index.html

 

static page(정적 페이지)

 

index.html 생성

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome Page</title>
</head>
<body>Welcome Page</body>
</html>

 

2. controller

@Controller
public class HelloController {
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data", "hello!");
        return "hello";
    }
}

 

resources > templates > hello.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <title>Hello</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>

 

<출처> 김영한 - 스프링 입문 강의자료

@getMapping("value") → localhost:8080/value

 

컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버( viewResolver )가 화면을 찾아서 처리한다.

스프링 부트 템플릿엔진 기본 viewName 매핑

resources:templates/ +{ViewName}+ .html

 

return "hello"; → resources > templates > hello.html

 

 

 

'Java , Spring > Spring' 카테고리의 다른 글

컴포넌트 스캔  (0) 2023.03.29
싱글톤 컨테이너  (0) 2023.03.23
스프링 컨테이너와 스프링 빈  (1) 2023.03.22
Spring Boot (3) 정적, MVC, API  (0) 2023.01.30
Spring Boot (1) 스프링 부트 시작하기  (0) 2023.01.30

댓글