KSI일기장
Spring Boot 기초2 Thymeleaf 페이지연결 본문
@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파일 없음 ->입력값 그대로 출력되었다.
'Spring' 카테고리의 다른 글
SpringBoot ModelAndView사용(타임리프에 DB값 넘겨주기) (1) | 2023.11.22 |
---|---|
Vo or Dto에서 쓰이는 어노테이션 (1) | 2023.11.21 |
SpringBoot 기초1 인텔리제이 스프링부트 설치 (0) | 2023.10.31 |
231031 ModelAndView, Model (0) | 2023.10.31 |
231031 JPA 이론 (0) | 2023.10.31 |