Spring - 프로젝트 환경설정(1)

1 분 소요

프로젝트 환경설정

프로젝트 생성

start.spring.io 페이지 접속 후 아래와 같이 설정 후 GENERATE 선택

image

group : 보통 기업의 도메인명

Aritfact: 프로젝트명

Dependencies - 프로젝트 시작할 때 어떤 라이브러리를 가져와서 쓸 것인지

  • Spring web
  • Thymeleaf (html을 만들어주는 템플릿 엔진)

스프링 실행

gradle 설정 (ctrl + alt + s)

Build and run using과 Run tests using 을 Intellij IDEA 로 설정해준다.

👉 Gradle을 통하지 않고 Intellij에서 자바를 통해서 바로 실행시켜서 더 빠르게 실행됨.

image

Intellij로 해당 프로젝트 실행 후 메인 자바 파일을 실행.

image

localhost:8080으로 웹 실행(톰캣이란 웹서버가 자체적으로 내장되어 있음)

image

Gradle 라이브러리 살펴보기

image

Gradle 은 의존관계가 있는 라이브러리를 함께 다운로드 한다.

스프링 부트 라이브러리

  • spring-boot-starter-web
    • spring-boot-starter-tomcat: 톰캣(웹 서버)
    • spring-webmvc: 스프링 웹 MVC
  • spring-boot-starter-thymeleaf : 타임리프 템플릿 엔진(View)
  • spring-boot-starter(공통): 스프링부트 + 스프링 코어 + 로깅
    • spring-boot
      • spring-core
    • spring-boot-starter-logging
      • logback, slf4j

테스트 라이브러리

  • spring-boot-starter-test
    • junit: 테스트 프레임워크
    • mockito: 목 라이브러리
    • assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
    • spring-test: 스프링 통합 테스트 지원

View 설정

페이지 화면 띄우기

src > main > resources > static > index.html 파일 생성

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
    <title>Hello</title>
</head>
<body>
<h1>Hello</h1>
<a href="/hello">hello</a>
</body>
</html>

image

작성한 index.html이 뜨는 것을 확인 할 수 있다.

thymeleaf 템플릿엔진 동작 확인

src > main > java > hello.hellospring > controller 패키지 생성

controller > HelloController 자바클래스 생성

package hello.hellospring.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("data", "hello!!");
        return "hello";
    }
}

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

<!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>
  • 컨트롤러에서 리턴 값으로 문자를 반환하면 viewResolver 가 화면을 찾아서 처리한다.
    • 스프링 부트 템플릿엔진 기본 viewName 매핑
    • resources: templates/ + {ViewName} + .html

image

실행화면

image

참고자료

인프런 - 스프링 입문 (김영한)

카테고리:

업데이트:

댓글남기기