[Spring 스프링] 2-2. MVC와 템플릿 엔진

2023. 10. 3. 14:12·[ Development ]/[ Back-end ] Spring 기본

* MVC와 템플릿 엔진

Model - View - Controller

 

이전에는 뷰와 컨트롤러를 구분하지 않고 하나로 진행했으나 

View는 화면을 구성하고 화면 관련된 것에

Controller는 로직, 내부적 처리하는 것에 집중하도록 한다

 

helloController에 

// 기존 내용
@Controller
public class HelloController {
 @GetMapping("hello")
 public String hello(Model model) {
 model.addAttribute("data", "hello!!");
 return "hello";
 }
}

// 하단의 내용 추가
@Controller
public class HelloController {
 @GetMapping("hello-mvc")
 public String helloMvc(@RequestParam("name") String name, Model model) {
 model.addAttribute("name", name);
 return "hello-template";
 }
}

외부에서 파라미터 받을 것

addAttribute(key, name)으로 구성

return 뒤에 명시된 곳으로 이동한다

 

templates 에 hello - templates.html 생성

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

타임리프의 장점은 파일 작성 후 서버 없이 바로 열어봐도 볼 수 있음

( 파일 우클릭 앱솔루트 카피 해서 주소 창에 파일 열고 소스 코드 확인 )

 

템플릿 엔진으로 동작하면 hello!empty가 th text로 치환된다 

 

 

public String helloMvc(@RequestParam(value = "name", required = true) String name, Model model) {

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

 

 

동작 방식

주소에서 name=spring!!!으로 넘어가면 컨트롤러에서 name = spring!!!으로 넘어가고 모델에 담겨

템플릿으로 넘어가면 ${name}은 모델의 키값이 name에서 값을 꺼내는 것

 

viewResolver 뷰를 찾아주고 템플릿 엔진을 연결해준다

 

 

 

728x90

'[ Development ] > [ Back-end ] Spring 기본' 카테고리의 다른 글

[Spring 스프링] 3. 회원 관리 예제 3 - 회원 리포지토리 테스트 케이스 작성  (1) 2023.10.03
[Spring 스프링] 3. 회원 관리 예제 2 - 회원 도메인과 리포지토리 만들기  (0) 2023.10.03
[Spring 스프링] 3. 회원 관리 예제 1 - 비즈니스 요구사항 정리  (0) 2023.10.03
[Spring 스프링] 2-3. API  (1) 2023.10.03
[Spring 스프링] 2-1. 정적 컨텐츠  (1) 2023.10.03
[Spring 스프링] 1-3. View 환경 설정 (Welcome Page 만들기)  (0) 2023.10.03
[Spring 스프링] 1. 프로젝트 환경 설정  (1) 2023.09.26
'[ Development ]/[ Back-end ] Spring 기본' 카테고리의 다른 글
  • [Spring 스프링] 3. 회원 관리 예제 1 - 비즈니스 요구사항 정리
  • [Spring 스프링] 2-3. API
  • [Spring 스프링] 2-1. 정적 컨텐츠
  • [Spring 스프링] 1-3. View 환경 설정 (Welcome Page 만들기)
dev charlotte
dev charlotte
주 - 컴퓨터공학 / 복수 - 산업 보안
    250x250
  • dev charlotte
    int main() {
    dev charlotte
  • 전체
    오늘
    어제
    • 분류 전체보기
      • [ Laboratory ]
        • Paper review
        • Advanced Operating System
        • System Software & Storage
        • Lab etc
      • [ Computer Science ]
        • Algorithm_C++
        • Operating System
        • Information Retrieval
        • Database_sql
        • SW Engineering
        • Computer Network
        • JavaScript
        • Python
        • Data Structure
        • CS study
        • Distributed systems
      • [ Computer Security ]
        • Convergence Security
        • Web Security
        • PIMS
        • Network Security
        • Digital Finance
      • [ Artificial Intelligence ]
        • Trend
        • Seminar
      • [ 미래, 같이, LG ]
      • [ Development ]
        • [ Front-end ]
        • [ Back-end ] Spring 기본
        • [ Back-end ] Node.js
      • etc
        • 현대오토에버 스마트 모빌리티 공학 체험 교육
      • It's me
  • 블로그 메뉴

    • 링크

      • GitHub
    • 공지사항

    • 인기 글

    • 태그

      공대생 대외활동
      코딩 교육
      MySQL
      티스토리챌린지
      현대오토에버
      현대오토에버 스마트모빌리티
      코딩 인강
      프로그래머스 입문
      싸피
      소프트웨어 공학
      비전공자 코딩
      오블완
      ssafy 13기
      ACM
      데이터베이스
      대학생 대외활동
      현대오토에버 스마트 모빌리티
      프로그래머스 c++
      스마트모빌리티공학체험교육
      자바스크립트
      SQL
      spdk
      프로그래머스 코테
      프로그래머스
      백준
      hotstorage
      코드잇
      프로그래밍 언어론
      SSAFY
      싸피 13기
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.4
    dev charlotte
    [Spring 스프링] 2-2. MVC와 템플릿 엔진
    상단으로

    티스토리툴바