의존성

첫 페이지 만들기

타임리프 페이지 만들기

<!DOCTYPE html>
<html lang="en" xmlns:th="<http://www.thymeleaf.org>">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${message}">hello</h1>
</body>
</html>

message 변수가 없으면 hello를 기본 텍스트로 사용

컨트롤러

@Controller
public class SampleController {

	@GetMapping("/")
	public String index(Model model) {
		model.addAttribute("message", "Hello Spring Security");
		return "index";
	}
}

추가해보자.

@Controller
public class SampleController {

	@GetMapping("/")
	public String index(Model model, Principal principal) {
		if (principal == null) {
			model.addAttribute("message", "Hello Spring Security");
		} else {
			model.addAttribute("message", "Hello " + principal.getName());
		}
		return "index";
	}

	@GetMapping("/info")
	public String info(Model model) {
		model.addAttribute("message", "Info");
		return "info";
	}

	@GetMapping("/dashboard")
	public String dashboard(Model model, Principal principal) {
		model.addAttribute("message", "Hello " + principal.getName());
		return "dashboard";
	}

	@GetMapping("/admin")
	public String admin(Model model, Principal principal) {
		model.addAttribute("message", "Hello Admin " + principal.getName());
		return "admin";
	}
}