웹 브라우저에서 http://localhost:8080/hello-spring.html 호출할 경우
→ 내장 톰캣 서버를 거쳐 스프링부트로 들어온다.
→ (1) 컨트롤러에서 찾는다. hello-spring이라는 컨트롤러가 있는가? ⇒ mvc방식 또는 api 방식
→ (2) resources 폴더에서 찾는다. hello-spring.html 파일이 있는가? ⇒ 정적 컨텐츠 방식
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
resources/templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
@Controller
public class HelloController {
@GetMapping("hello-string")
**@ResponseBody**
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
}
==> 문자 그대로를 반환한다.
@Controller
public class HelloController {
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello; ===> Hello 객체를 반환하고 있다.
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
| TestSuiteExecutionException: Could not execute test class 해결방법 (0) | 2024.12.12 |
|---|---|
| Bean 수동 등록, Bean 주입 우선순위 - @Qualifier와 @Primary (0) | 2024.12.10 |
| ✍🏻IoC(제어의 역전), DI(의존성 주입) (0) | 2024.12.04 |
| Spring Bean (0) | 2024.12.01 |
| 🔧Intellij - spring run 아이콘 비활성화 (0) | 2024.11.22 |