나의 브을로오그으

#1-2. [스프링 입문] - 프로젝트 설정 본문

Spring

#1-2. [스프링 입문] - 프로젝트 설정

__jhp_+ 2022. 7. 9. 21:17

[resources/templates/hello.html]

 

[hellospring/controller/HelloController]

 

[실행]

스프링 애플리케이션 실행 -> 웹브라우저 주소창에 입력 : localhost:8080/hello

 

[동작원리]

1. localhost:8080에서 /hello라고 웹브라우저에서 서버로 전달

 

2. 스프링 부트에는 톰켓이라는 서버를 내장하고 있다. 따라서 서버에서 이걸 받아서 /hello가 있는지 스프링 컨테이너의 Controller에 @GetMapping("hello") = HTTP GET Request임. 이 문자열(url)에 매핑이 되어 있어서 hello()메소드가 실행된다.

 

3. 이때 model이라는 파라미터는 Spring Container에서 인자로 넘겨주는 것임. 여기다가 model.addAttribute()메소드를 호출해서 'attributeName'인 data에 'attributeValue'로 hello!!를 넘겨줌. 그리고 hello를 return함.

 

4. return 한 문자열에 해당하는 html 파일을 viewResolver가 templates패키지에서 찾아서 템플릿 엔진(Thymeleaf)이 처리하게 됨.

 

5. 이것을 웹브라우저에서 랜더링

 

※ `spring-boot-devtools` 라이브러리 추가 시 `.html` 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경 가능

인텔리J 컴파일은 build -> Recompile

 

[콘솔에서 뷜드 -> 실행]

1. 콘솔로 이동

2. ./gradlew build 

3. cd build/libs

4. java -jar hello-spring-0.0.1-SNAPSHOT.jar

5. 실행 확인

 

서버 배포 : [SpringApplicationName]-[Version]-SNAPSHOT.jar만 배포하면 됨.

(만약 안될경우 프로젝트디렉터리에 가서 gradlew build clean한 후 다시 뷜드 진행)