Post

Spring 스프링 입문 - 1. 프로젝트 환경설정

프로젝트 환경설정

View 환경설정

  • Welcome Page 만들기
  • static/index.html을 올려두면 Spring boot가 자동으로 Welcome Page 기능을 제공한다.

resources/static/index.html

1
2
3
4
5
6
7
8
9
10
11
  <!DOCTYPE HTML>
  <html>
  <head>
   <title>Hello</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  Hello
  <a href="/hello">hello</a>
  </body>
  </html>

resources/templates/hello.html

1
2
3
4
5
6
7
8
9
10
<!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>
1
2
3
4
5
6
7
8
@Controller
public class HelloController {
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","hello!!");
        return "hello";
    }
}
  • 실행: http://localhost:8080/hello

img

  • 컨트롤러에서 @GetMapping(“hello”)를 통해 주소 값 뒤인 hello를 매핑하여 hello 메서드를 실행한다.
  • 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버( viewResolver)가 template에서 화면을 찾아서 처리한다.
    • 스프링 부트 템플릿엔진 기본 viewName 매핑
    • resources:templates/ +{ViewName}+ .html

References: 김영한 - [스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술]
This post is licensed under CC BY 4.0 by the author.