-
인프런 스프링 입문 03 / 정적 콘텐츠, MVCSpring&SpringBoot 2022. 12. 2. 23:48
스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술
스프링 웹 개발 기초
정적 콘텐츠
- 스프링부트는 정적 콘텐츠 기능을 자동으로 제공 > 7.5.1. Static Content
<!DOCTYPE HTML> <html> <head> <title>Static Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> 정적 컨텐츠 입니다. </body> </html>
- 주소 : http://localhost:8080/ + 파일 이름
예) http://localhost:8080/hello-static.html
MVC와 템플릿 엔진
- MVC : Model, View, Controller
<p th:text="'hello ' + ${name}">hello! empty</p>
- thymleaf 템플릿 장점 : html을 서버 없이 파일을 열어볼 수 있음
> copy absolute path > C:\Users\USER\Desktop\study\hello-spring\src\main\resources\templates\hello-tempalte.html
> 동작을 하게 되면 <p th: "내용 1"> 내용 2 </p> 내용 2가 내용 1로 대체됨
> 내용2는 없어도 됨
package hello.hellospring.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @GetMapping("hello-mvc") public String helloMVC(@RequestParam("name") String name, Model model) { model.addAttribute("name", name); return "hello-template"; } }
<html xmlns:th="http://www.thymleaf.org"> <body> <p th:text="'hello ' + ${name}">hello! empty</p> </body> </html>
- 2022-12-02 23:34:37.825 WARN 19832 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.bind.MissingServletRequestParameterException:
Required request parameter 'name' for method parameter type String is not present
> 파라미터가 없어 에러 발생
> http://localhost:8080/hello-mvc?name=jy 파라미터 넘겨주면 해결
728x90'Spring&SpringBoot' 카테고리의 다른 글
인프런 스프링 입문 05 / 회원 관리 예제, junit 테스트하는 방법 (0) 2022.12.04 JSON이란? JSON 기초 정리 (1) 2022.12.03 인프런 스프링 입문 02 / 간단한 실행 (0) 2022.12.01 인텔리제이 윈도우 gradle build하는 방법, gradlew build 오류 해결 방법 (2) 2022.12.01 인프런 스프링 입문 01 / 인텔리제이 설치, 스프링부트 localhost 로그인, 인텔리제이 윈도우 preference (0) 2022.11.29