본문 바로가기
Programming/Java

Spring boot - Thymeleaf 적용하기

by Wilkyway 2024. 4. 8.
반응형

 

1. template/hello.html

데이터를 받아들일 html템플릿을 만들어둔다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello</title>
</head>
<body>
    <p th:text="${user}">Hello Thymeleaf!!</p>
</body>
</html>

 

2.Controller.java

자료를 넘길때는 ModelAndView객체에 담아서 보낸다.

...
	
@GetMapping("/user")
public ModelAndView hello(@RequestParam("id") String id) throws Exception {

    ModelAndView mav = new ModelAndView();		
    UserDto res = userService.getUserById(id);

    mav.setViewName("hello");	
    mav.addObject("user",res.getName());

    return mav;
}
    
 ...
반응형

댓글