스프링

MVC와 템플릿 엔진

hmjhaha 2024. 1. 10. 16:31

- 화면을 그리는데에 모든 역량 집중

컨트롤러, 모델 - 비즈니스 로직과 관련이거나 내부적으로 처리하는데 집중

/hello-spring/src/main/java/hello/hellospring/controller/HelloController.java

 

package hello.hellospring.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 HelloController{
      
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name",name);
        return "hello-templates";
    }
}

 

/hello-spring/src/main/resources/templates/hello-templates.html

 

<html xmlns:th = "http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 

url : http://localhost:8080/hello-mvc?name=spring! 입력시   hello spring! 출력