프로젝트 환경설정

임성준·2022년 1월 26일
0
post-thumbnail

Spring Initializr를 통한 세팅

세팅

Project : gradle
Language : Java
Spring Boot : 안전한 버전 선택
Packaging : Jar
Java : 11 (공부 당시 버전)
Dependencies : Spring Web, Thymeleaf(템플릿 엔진)
Group : hello (기업 도메인 명)
Artifact : hello-spring (빌드의 결과물, 프로젝트 명)

서버 실행

메인 메소드 실행 (Run)

src > main > java > hello > heelospring > HelloSpringApplication.java

View 환경 설정

src > main > resources > static > index.html(생성 // welcome 페이지)

💎 위 경로 내에 index.html 파일을 자동으로 잡을 수 있도록 세팅되어있다.

Thymeleaf 화면 설정

controller 생성 - 라우터 설정
src > main > java >hello.hellospring.controlloer.helloController.java

package hello.hellospring.controller;

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

@Controller // view(화면) return이 주목적인 annotation
public class helloController {
	
// @GetMapping : GET 요청 방식의 API를 만들 때 사용
// @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping 등 도 있다.

    @GetMapping("hello") 
    public String hello(Model model) { // xml Model 타입, 인자명 model
    	
        // Model addAtrubute("key", "value"); 
        // 뷰코드에 key를통해 값을 전달
        
        model.addAttribute("data", "hello!");
        return "hello"; 
        
// View 리턴 
// templates 패키지에 hello.html파일을 위 코드에 설정한 주소로 실행시킨다.

    }
}

View 생성

src > main > resources > templates > hello.html(생성)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> // Thymeleaf 사용 표시
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="'안녕하세요. ' +${data}">안녕하세요. 손님</p>
</body>
</html>

빌드

  1. Project 안에서 ./gradlew build
  2. build/libs 폴더로 이동 cd build/libs
  3. java -jar hello-spring-0.0.1-SNAPSHOT.jar 실행
    이후 서버가 실행된다.
  • 오류시 Preoject 내에서 ./gradlew clean build 를 실행하게되면 build폴더를 지우고 다시 build하게된다.
profile
오늘도 공부 📖🌙

0개의 댓글