Spring/SpringBoot

Spring Boot으로 웹출시 - thymeleaf를 이용한 화면 작성

느리지만 꾸준하게 2022. 5. 30. 11:32

여기서 진행한다.

 

Serving Web Content with Spring MVC title을 참고하여 만들어 주었다.

 

  • Thymeleaf

 

  • Spring Web

 

  • Spring Boot DevTools

 

 

 

세 개의 Dependencies를 ADD해주고

project를 generate 해주자.

Spring initializer setting

 

GreetingController 패키지를 만들어주고 class를 작성해준다.

package com.example.mythymeleaf.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}

 

 

그리고 여기서 부터는 vscode로 진행하면서 확인 해주자.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

 

 

GreetingController Class의 코드를 반영하여

package com.example.mythymeleaf.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(@RequestParam(name="name", required=false, defaultValue="Jay") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}

 

 

 

MythymeleafApplication의 코드를 돌리면 결과를 확인할 수 있다. 이렇게 thymeleaf를 이용해서 간단한 문법과 프로젝트를 생성해 보았다.

package com.example.mythymeleaf;

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

@SpringBootApplication
public class MythymeleafApplication {

   public static void main(String[] args) {
      SpringApplication.run(MythymeleafApplication.class, args);
   }

}

 

 

 

 

 

<코딩의 신 - Spring Boot으로 웹 출시까지 #2. thymeleaf를 이용한 화면 작성>

https://www.youtube.com/watch?v=gSTmoremRBU&list=PLPtc9qD1979DG675XufGs0-gBeb2mrona&index=2