이 코드는 사용자가 웹사이트의 첫 화면(http://localhost/)에 접속했을 때, list.html이라는 템플릿을 보여주는 스프링 컨트롤러입니다.
스프링에서는 @Controller와 @GetMapping 어노테이션을 사용하여, 특정 URL 요청이 들어왔을 때 어떤 화면을 보여줄지를 지정할 수 있습니다.
com.example.memo 패키지에 MemoController라는 이름의 클래스를 만들고, 아래 코드를 입력합니다.
▼ Code
package com.example.memo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MemoController {
@GetMapping("/")
public String list() {
return "list";
}
}
▼ Explain
@Controller 이 클래스가 스프링 웹 애플리케이션에서 요청을 처리하는 컨트롤러 클래스임을 나타냅니다. @GetMapping("/") 사용자가 웹사이트의 루트 주소(/) 로 접속했을 때 실행될 메서드임을 지정합니다. 즉, http://localhost/ 요청을 처리합니다. public String list() { 위 요청이 들어왔을 때 실행되는 메서드입니다. 반환값은 보여줄 HTML 파일의 이름입니다. return "list"; resources/templates/list.html 파일을 찾아 브라우저에 보여주라는 의미입니다. .html 확장자는 생략합니다.
개인정보처리방침 · 이용약관