Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

Spring Boot 기초2 Thymeleaf 페이지연결 본문

Spring

Spring Boot 기초2 Thymeleaf 페이지연결

MyDiaryYo 2023. 11. 3. 00:14

 

 

 

@Controller
public class HelloController {
    
    //1번
    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hellowwwww~");
        return "hello";
    }

    //2번
    @GetMapping("hello-mvc")
    public String hellomvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }
    
    //3
    @GetMapping("hello-api")
    @ResponseBody
    public Hello helloapi(@RequestParam("name") String name){
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }
    
    static class Hello {
        private String name;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

 

 

1번 Thymeleaf   hello.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
    <p th:text=" '안녕하새우' + ${data}">안녕하새우~깡!</p>
</body>
</html>

 

 

2번 Thymeleaf   hello-template.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
<p th:text=" '안녕하새우  ' + ${name} + 입니다.">안녕하새우~깡</p>
</body>
</html>

 

 

1번 결과

  --> model.addAttribute("data", "hellowwwww~");  ${date}에 hellowwwww가 들어왔다.

 

 

 

 

2번 결과

--> @RequestParam으로 얻어온 name값이 ${name}에 들어갔다.

 

 

 

 

 

3번 결과

@ResponseBody로 인해 결과가 키:값 (Json형태)의 형태로 나왔다. 

html파일 없음 ->입력값 그대로 출력되었다.