YUIN

MVC와 템플릿 엔진 본문

Spring/Spring <입문>

MVC와 템플릿 엔진

유_인 2023. 4. 8. 03:22

 

- MVC: Model, View, Controller

View와 Controller를 쪼개서 운영함

 

- View: 화면과 관련된 일 처리

- Controller: business logic, server 뒷단과 관련된 일들 처리

 

>controller>HelloController

아래 코드 작성

@Controller
public class HelloController {
 @GetMapping("hello-mvc")
 public String helloMvc(@RequestParam("name") String name, Model model) {
 model.addAttribute("name", name);
 return "hello-template";
 }
}

>templates>hello-template.html

아래 코드 작성

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 

입력 후 localhost:8080/hello-mvc 접속 시 에러 

 

로그 확인

parameter name에 값 전달 해줘야 함

 

=> 주소창에 

localhost:8080/hello-mvc?name=spring!

이런 식으로 ?name="" 값 전달 해주면 됨

?는 http식의 and, 따라서 "그리고 이름은 "와 동치

리턴값은 "hello"+""식으로 나타남

 

- 정적 컨텐츠와의 차이점

: 정적 컨텐츠는 viewResolver가 변환 과정을 거치지 않고 웹브라우저로 전달해주었지만,

MVC에서는 viewResolver가 변환 과정을 거치고 웹브라우저로 전달해줌. 

 

입력한 html

 

 

html 반환값

웹페이지의 소스 코드를 확인해 보면 spring!이라는 name이 전달되어 변환된 상태로 출력되는 것을 확인할 수 있음

'Spring > Spring <입문>' 카테고리의 다른 글

회원 관리 예제- 백엔드 개발  (0) 2023.04.10
API  (0) 2023.04.10
정적 컨텐츠  (0) 2023.04.08
스프링 웹 개발 기초  (0) 2023.04.08
View 환경설정  (0) 2023.04.05