4. SpringBoot - MVC

한승록·2023년 11월 27일
0

스프링 부트 기초

목록 보기
4/6

1. View(Thymeleaf)

Gradle 프로젝트에서는 .jsp 파일이 아닌 .html 으로 생성합니다.

생성위치는 위의 그림과 같이 srcmainresourcesstatic 아래 생성하게 됩니다. JSP에서는 보통 home.jsp 로 시작페이지를 구성하였지만 Thymeleaf사용을 위해서는 index.html 로 생성합니다.
<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <a href="/hello">hello</a>
</body>
</html>



2. Controller

Maven 프로젝트에서는 com.식별자.controller 으로 package를 생성할 경우 servletcontext.xmlcontroller가 등록되어야 작동이 가능했습니다.
하지만 Gradle 기반의 SpringBoot에서는 해당 프로젝트 패키지 아래에 controller 패키지가 새로 생성됩니다.

코드 작성은 아래와 같습니다.

@Controller
public class HelloController {
	
    @GetMapping("hello")
    public String hello(Model model) {
    
    	model.addAttribute("data", "hello!!");
    	return "hello";
    }
}
<!DOCTYPE html>
<html>
<html xmlns:th="http://www.thymeleaf.org">
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
	<p th:text="'안녕하세요, ' + ${data}">안녕하세요. 손님</p>
</body>
</html>

<※ 결과는 다음과 같습니다.>



3. Model

일반적인 SpringFramework와 사용방식이나 개념적인 면에서 차이를 보이는 것은 없습니다.
전통적으로 수행하던 데이터를 받아 저장하고 ViewController에 저장된 데이터를 주고받도록 하는 역할을 똑같이 수행하게 됩니다.
profile
개발 학습

0개의 댓글