Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

View 환경설정 본문

인프런/[인프런] Springboot와 JPA활용 1

View 환경설정

Seung__ 2021. 8. 5. 14:06

1. Thymleaf 사용


  • template 엔진으로 Thymleaf를 사용
  • template 엔진? 템플릿과 자료를 합성하여 사용자가 볼 수 있도록 렌더링 해줌

 

 

 

2. Controller 


  • 간단한 controller를 구성
@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){

        model.addAttribute("data", "hello!");
        return "hello"; // 화면이름, html 파일명, resource의 templates하위.

    }
}
  • Model??
    - model에 data를 실어서 Controller를 통해 view로 넘길 수 있음
    - 생성된 data를 view로 보낼 때 사용하는 객체
    - attribute(key, value)형태로 데이터를 전달함.
    - 여기서는 attribute의 name을 data, attribute의 value를 hello! 지정.
  • Controller에서 String을 반환하는 경우
    - 해당 이름과 일치하는 html과 연결됨 by Springboot
    - 단, resources/templates 경로의 html과 연결됨.

 

 

3. hello.html


  • localhost:/8080/hello 접속 시 보여질 페이지.
<!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>
  • th(Thymeleaf)가 Model로 건네받은 데이터와 html을 렌더링 함.
  • ${data}는 model로 받은 attribute와 대응된다.
    data = attributeName = data = key, ${data} =attributeValue = hello! = value
  • 만약 서버를 띄우지 않고 thymeleaf가 돌아가지 않으면 "안녕하세요. 손님" 출력됨.
  • 실행 결과. 
    - 안녕하세요  + attributeValue 값이 출력되었다.

 

 

4. index.html


  • welcome page
  • localhost://8080 으로 접속했을 때 보여지는 페이지
  • 링크를 통해 localhost://8080/hello 접속 가능.
<!DOCTYPE HTML>
<html>
<head>
  <title>Hello</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello. Welcome
<a href="/hello">hello</a>
</body>
</html>

 

 

 

 

5. devtools


html 수정이 빈번한데 매 버 서버 다시 띄우기 귀찮음..

implementation 'org.springframework.boot:spring-boot-devtools'

restartedMain이 확인되면 devtools셋팅이 완료된 것이다.

html 수정하고 recompile만 실행하면..? 바로 반영이 된다.

ㄱㅇㄷ

 

6. GitHub : 210805 View


 

GitHub - bsh6463/SpringBootJPA1

Contribute to bsh6463/SpringBootJPA1 development by creating an account on GitHub.

github.com

 

'인프런 > [인프런] Springboot와 JPA활용 1' 카테고리의 다른 글

엔티티 클래스 개발  (0) 2021.08.05
도메인 모델 , 테이블 설계  (0) 2021.08.05
도메인 분석 설계  (0) 2021.08.05
JPA, DB 설정 및 실행하기  (0) 2021.08.05
H2 Datebase  (0) 2021.08.05
Comments