JAVA/JAVA개발자 양성과정

[SpringMVC] SpringBoot, thymeleaf template 사용하여 개발하기

👩‍💻 자바 개발자 양성과정 64일차

1. mvc가 왜 좋은지?

2. SpringBoot + thymeleaf template 사용하기

3. 이클립스 디버그 내용 더 자세하게 보는 법

4. Link식과 href

5. SpringBoot 메세지 포멧팅

 


💦 mvc가 왜 좋은지?

MVC 패턴을 쓰는 이유 예제를 예제로 알아보자. (tistory.com)

 

MVC 패턴을 쓰는 이유 예제를 예제로 알아보자.

이 글은 MVC패턴을 모르는 사람도 쉽게 이해 할 수 있도록 도움을 주기 위하여 작성되었다. MVC패턴의 사전적 정의라던지 하는 내용은 다루지 않는다. (위키사전에 검색하면 매우 상세히 나옵니

jhleed.tistory.com

 


💦 SpringBoot + thymeleaf template 사용하기

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

	@GetMapping("/")
	public String index() {
		return "index"; //만약 index.html라면 무조건 static/index.html 을 찾고
		//만일 index라고만 쓰면 templates/index.html을 찾는다.
	}
}

SpringBoot로 인해 

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	메인페이지
	<p th:text="${new java.util.Date().toString()}"></p>
</body>
</html>

 


💦 SpringBoot 메세지 포멧팅

messages.properties에서 값을 가져옴

 

messages.properties

content.title=Message sample page.
content.message=This is sample message from properties.

#{ }

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<p th:text="#{content.title}">link</p>
	<p th:text="#{content.message}">link</p>
</body>
</html>

 


💦 SpringBoot - Link식과 href

동적으로 링크 설정하기

컨트롤러를 거치지 않고 설정할 수 있다.

{orderId}(orderId가 무슨 값인지 설정해줌)

 

@{ }

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<p>
		<a th:href="@{/home/{orderId}(orderId=${param.id[0]})}">link</a>
	</p>
</body>
</html>

 


💦 SpringBoot 디버그 내용 더 자세하게 보는 법

application.properties

logging.level.root=warn
logging.level.org.springframework.web=debug

변경 전

 

변경 후

728x90
반응형