Spring/SpringBoot

프로젝트 환경설정 - View 환경 설정

느리지만 꾸준하게 2022. 3. 30. 00:13

main폴더에 jpabook.jpashop폴더에서 HelloController Class를 만들어준다.

 

@Controller annotation을 달아준다.

hello라는 url로 오면 Controller가 호출되겠다고 하는 것이다.

model에 아무 값이나 담는다

Model model에 데이터를 실어서 view에 넘길 수 있게 해준다.

data key의 값을 hello라는 걸로 넘길거고

// HelloController

package jpabook.jpashop;


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";
    }
}

 return hello은 화면 이름인데 관례상 return "hello.html";로 자동으로 붙는다.

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>

이렇게 작성을 해주면 아래와 같이 렌더링이 된다.

Browser / 소스보기

이제 SpringBoot main을 실행하면(JpashopApplication) spring boot가 내장 톰캣을 가지고 돌려준다.

// JpashopApplication

package jpabook.jpashop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpashopApplication {

	public static void main(String[] args) {

		SpringApplication.run(JpashopApplication.class, args);
	}

}

 

이제 정적인 페이지를 만들어보면 static폴더안에 index.html파일을 만들고 중지하고 다시 돌려주면

 

// index.html

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

 

아래와 같이 정적 컨텐츠가 만들어지게 된다.

 

 

 

라이브러리 중에 devtools라는 라이브러리를 build.gradle에 추가를 해주고 임포트를 해주면 캐싱과정을 없애고 리로딩이 다 되게 만들어준다.

	implementation 'org.springframework.boot:spring-boot-devtools'

 

다시 한번 돌리고 build에 Recompile을 누르게 되면 새로고침을 누르면 바로 렌더링이 된다.

 

 

 

<출처 김영한: 실전! 스프링 부트와 JPA 활용1 - 웹 어플리케이션 개발 >

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-JPA-%ED%99%9C%EC%9A%A9-1/dashboard

 

실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발 - 인프런 | 강의

실무에 가까운 예제로, 스프링 부트와 JPA를 활용해서 웹 애플리케이션을 설계하고 개발합니다. 이 과정을 통해 스프링 부트와 JPA를 실무에서 어떻게 활용해야 하는지 이해할 수 있습니다., - 강

www.inflearn.com