본문 바로가기
개발/spring

spring view 환경설정

by 향유 2022. 11. 24.

교육자료

https://www.youtube.com/watch?v=P6AgXuh-fxA&list=PLumVmq_uRGHgBrimIp2-7MCnoPUskVMnd&index=4 

 

<!DOCTYPE HTML>
<html lang="ko">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
Hello
<a href="/hello">hello</a>

</body>
</html>

static 에 index.html 로 메인 페이지 기본 view 생성

화면 좌측 하단 정지 실행으로 서버 재실행 후 확인

spring.io -> project-> spring boot -> learm -> 2.6.13 ->  Reference Doc

버전별 제작 문서 확인 가능

 

동적 페이지 제작

 

package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    // /hello 로 get 주소 입력시 아래 소스 매핑하라는 명령어
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","hello!!"); //모델에 정보담아서 세팅
        return "hello"; //hello.html로 model 데이터 전달 명령어
    }
}

java/hello.hellospring/controller/HelloController 제작

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
 <p th:text="'안녕하세요.'+${data}">안녕하세요. 손님</p>
</body>
</html>

templete/hello.html 제작

컨트롤러 return 시 뷰 리졸버가 리소스/templates에 / "뷰네임 변수" .html 로 데이터 전달!

 

 

데이터 보내느 형식

 

    public String hello(Model model){
        model.addAttribute("data","hello!!"); //모델에 정보담아서 세팅
        return "hello"; //hello.html로 model 데이터 전달 명령어
    }

 

 

데이터 받는 형식


 <p th:text="'안녕하세요.'+${data}">안녕하세요. 손님</p>

댓글