resources 폴더 안에 있는 static 폴더 안에 html 파일을 만들어 주면 그 폴더가 실행된다.
하지만 이는 정적인 움직임이 가능하다.
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>
동적인 것을 만들려면 templates에 폴더 안에 html 파일을 만든다.
자바 파일과 함께 동작시킬 수 있다.
컨트롤러에서(mvc 모델의 컨트롤러) 리턴 값으로 문자를 반환하면 뷰 리롤버가 화면을 찾아서 처리한다.
스트링 부트 템플릿 엔진 ViewName으로 매핑하고 resources : templates/ViewName/ .html로 인식
Controller로 사용할 클래스에는 @Controller를 사용해준다.
return hello는 templates안에 있는 hello.html로 인식한다.
HelloController.java
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","스프링부트에요");
return "hello"; //입력 제대로 해야함
}
}
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>
터미널에서 빌드하기
cd 명령어로 spring 폴더 안으로 이동한다.
windows에서는
gradlew 입력
gradlew build 를 입력한다.
cd 명령어를 이용해 build/libs로 이동한다.
dir 명령어를 사용하면 생긴 .jar 파일이 있을 것이다.
이 것을 복사하고 java -jar 복사한 파일
을 하면 SPRING 이 나오며 8080포트가 실행된다.
이런 오류가 뜰 수 있다.
exception in thread "main" java.lang.unsupportedclassversionerror: hello/hellospring/hellospringapplication has been compiled by a more recent version of the java runtime (class file version 55.0), this version of the java runtime only recognizes class file versions up to 52.0
자바 1.8 버전을 사용해왔고 저번 시간에 인텔리제이에서는 변경을 해주었지만 컴퓨터의 환경변수는 건드리지 않았다.
그래서 인텔리제이에서 다운 받은 C:\Users\User\.jdks\corretto-11.0.14.1 이 곳으로 JAVA_HOME의 위치를 변경했다.
하지만 이 위치를 인식하지 못했고, 자바 1.8을 백업 해둔 뒤, 자바 1.8을 모두 삭제하고 자바 11을 다시 다운 받아 JAVA_HOME을 다시 설정했다.
'🍀spring > 스프링 입문' 카테고리의 다른 글
[스프링 웹 개발 기초] 정적 컨텐츠, MVC와 템플릿 엔진, API (0) | 2022.03.19 |
---|---|
[스프링 부트] 테스트 라이브러리, 스프링 부트 라이브러리 (0) | 2022.03.18 |
[프로젝트 환경 설정] 스프링 입문 - 코드로 배우는 스프링 부트 (0) | 2022.03.17 |