의존성 설정

build.gradle

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}

스프링 부트 라이브러리

  • 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

 

  Welcome Page

   

resources/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 Spring
</body>
</html>

 

http://localhost:8080/

 

 

@Controller

 

 

HelloController.java

@Controller
public class HelloController {

    @GetMapping("hello") //http://localhost:8080/hello
    public String hello(Model model){
        model.addAttribute("data","hello");
 
        return "hello";    //   resources/templates/hello.html
    }
    
    
    }

 

resources/templates/hello.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>

 

${data}  <=  HelloContrller 에 Model model 에 있는 key 값  (value는 hello)

 

 

 

 

 

 

 


 

MVC와 템플릿엔진

 

HelloController.java

 
 @Controller
public class HelloController {
 
 @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("NAME") String name , Model model){

         model.addAttribute("name", name);

        return "hello-template";  //resources/templates/hello-template.html
    }
    
   }

 

 

http://localhost:8080/hello-mvc?NAME=""

 

@RequestParam("NAME")  <=  ( ?NAME= )

 

 

@RequestParam의 값이 Model 의 value가 된다.

 

 

 

resources/templates/hello-template.html

 

hello-template.html

<html xmlns:th="http://www.thymleaf.org" >
<body>

<p th:text="'안녕 내이름은' +${name} + '야!!'" >hello! empty</p>

</body>

</html>

 

실행

http://localhost:8080/hello-mvc?NAME=park

 

 

 

 

 

 

 

출처 www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8

+ Recent posts