본문 바로가기
Spring Boot

[프로젝트 환경설정] view 환경설정 및 빌드

by sharpsim 2022. 1. 9.

2022.01.08 - [Spring Boot] - [프로젝트 환경설정] 프로젝트 생성

 

[프로젝트 환경설정] 프로젝트 생성

개발 환경 윈도우 Java SE 11 IntelliJ 프로젝트 생성 새로운 프로젝트를 직접 생성할 수 있지만, 여기서는 스프링 부트 스타터(https://start.spring.io/)를 이용하여 스프링 프로젝트를 생성한다. 설정은

sharpsim.tistory.com

개발 환경

  • 윈도우
  • Java SE 11
  • IntelliJ

view 환경설정

Welcome Page 생성

스프링 부트는 index.html을 Welcom page 기능으로 제공한다. 아래 위치에 index.html 파일을 생성한다.

  • resources/static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    Hello
    <a href="/hello">hello</a>
</body>
</html>

 

  • 패키지명.controller/HelloController.java
package com.example.hello.hello.controller;

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

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("name", "김철수");
        return "hello";
    }
}

 

  • resources/templates/hello.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <p th:text="'Hello, ' + ${name}" >Hello!</p>
</body>
</html>

 

thymeleaf 템플릿엔진 동작 확인

http://localhost:8080과 http://localhost:8080/hello를 실행해보면 다음 화면과 같이 나타나는 것을 확인할 수 있다.

실행 원리

 


빌드

  1. 명령 프롬프트(cmd) 실행
  2. 프로젝트가 저장된 경로로 이동
  3. gradlew.bat build 실행
  4. cd build/libs
  5. java -jar 파일명: IntelliJ와 같은 포트를 사용하므로 실행 전에 IntelliJ는 실행 중지