정적 컨텐츠
resources > static에 있는 파일명을 그대로 가져옴
동적 컨텐츠
MVC : Model / View / Controller
템플릿 엔진 : Html을 동적으로 만드는 역할 (Thymeleaf)
Thymeleaf의 기능 중 파일을 우클릭해서 절대 경로를 가져올 수 있음
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(value = "name", required = false) String name, Model model){
model.addAttribute("name",name);
return "hello-template";
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>HTML5</title>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
</head>
<body>
<!--th:text는 Controller로부터 값을 가져왔을때, 태그 안에 있는 값은 값이 없을때 표기된다-->
<p th:text="'안녕하세요. ' + ${data}">안녕하세요. 손님</p>
</body>
</html>
API
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello " + name;
}
@ResponseBody를 입력해주면 View를 내려주는게 아니라 문자열 그대로 떨굼
@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;
}
}
이런식으로 객체를 리턴하게 되면 아래와 같이 JSON으로 바로 떨궈준다
'SpringFramework > SpringBoot 기초' 카테고리의 다른 글
[SpringBoot] 스프링 빈과 의존관계 - 자동의존관계(컴포넌트 스캔), 자바코드로 직접 등록하기 (0) | 2021.12.07 |
---|---|
[SpringBoot] 스프링 백엔드 개발 예제 - 회원 서비스 (0) | 2021.12.06 |
[IntelliJ] 모든 단축키 모음 (0) | 2021.12.04 |
[SpringBoot] Spring Boot 맛보기 (2) - View & Build (0) | 2021.12.01 |
[SpringBoot] Spring Boot 맛보기 (1) - 초기 프로젝트 설정 (0) | 2021.12.01 |
댓글