View 화면 설정

김현지·2023년 5월 17일
0

Spring

목록 보기
3/5

개발 환경

  • Language : Java 11
  • Framework : Spring Boot 2.7.11
  • Build Tool : Gradle
  • Packaging : Jar
  • Dependencies : Spring Web, Thymeleaf, Lombok, Spring Boot DevTools

Welcome Page 코드 작성

  1. src > resources > static에 index.html이라는 파일명으로 html 파일 작성합니다.
    static : 고정적으로 유저에게 화면을 랜더링(그려주는)하는 소스코드의 경로, 폴더
<!DOCTYPE HTML>
<--! spring welcome page html 혹은 
     spring index.html이라는 키워드로 검색해서 복사해왔습니다.-->
<html>
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    Hello
  <--! 클릭 시 url의 끝에 /hello 추가되며 페이지 이동 -->
    <a href = "/hello">hello</a>
</body>
</html>

  1. src > main > java > com.example.hellospring > controller(패키지 생성) > helloController(클래스 생성)
package com.example.hellospring.controller;

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

@Controller
public class HelloController {
// URL이(Get방식, hello) 들어오면 이 메소드 호출
    @GetMapping("hello") 
    public String hello(Model model){
        // hello!! 대신 변수를 넣어 DB에서 가져오도록 할 수도 있다.
        model.addAttribute( "data","hello!!");

        return "hello";
        // viewResolver가 resources/templates 안에 있는 hello.html을 찾아서 렌더링한다.

        /* 스프링 부트 템플릿엔진 기본 viewName 매핑은
        resources:templates/ + {viewName} + .html 로 되어있다*/
    }

}

0개의 댓글

관련 채용 정보