Spring Boot - View 환경설정

범수·2024년 2월 11일

목차

  1. 프로젝트 생성하기
  2. View 환경설정
  3. 스프링 웹 개발 기초
  4. 회원 관리 예제
  5. 스프링 빈과 의존 관계
  6. 웹 MVC 제작
  7. DB 제작(1)
  8. DB 제작(2)
  9. AOP

1. Welcome Page 만들기

인덱스 페이지 만들기

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

Resources > static > index.html 추가

  • 8080포트에 접속 시 "hello"라는 문구 확인 가능
  • static 파일에 정적 페이지(index.html) 제작 가능

thymeleaf 템플릿 엔진 동작

 @Controller
 public class HelloController {
    @GetMapping("hello")
 public String hello(Model model) {
        model.addAttribute("data", "hello!!");
 return "hello";
    }
 }
 // Java > controller > HelloController.java
 <!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>
 // resources > templates > hello.html

model의 <data, hello!!>가 hello.html에서 반환되는 것을 확인할 수 있다.

  • 컨트롤러의 리턴 값에 대응하는 html에 동작하는 것을 알 수 있다.
  • 컨트롤러 >> return hello, model(data: hello!) >> viewResolver >> hello.html

CMD창으로 실행해보기

gradlew.bat 실행build>libs 이동
java -jar 실행

8080/hello 화면
  • cmd창에서 프로젝트 파일로 이동한 뒤 gradlew.bat(window시) -> build -> libs 순으로 이동하다 보면 hellow-spring-0.0.1-SNAPSHOT.jar를 찾을 수 있다.
  • java -jar hellow-spring-0.0.1-SNAPSHOT.jar를 실행하게 되면 IntelliJ에서 보았던 화면이 보이게 된다.
profile
범수의 개발 놀이터😋

0개의 댓글