0809 개발일지

Yesol Lee·2022년 8월 9일
0

개발일지 - 2022

목록 보기
112/187

오늘 한 일

프로젝트

  • 금요일 화면설계 회의 내용 반영하여 ppt 수정

사이드 프로젝트

프로젝트 환경 설정

1. gradle 라이브러리 의존관계

  • springboot starter를 사용하면 스프링 웹 프로젝트에 필요한 라이브러리를 알아서 가져와 준다.

<스프링 부트 라이브러리>

  • spring-boot-starter-web
    • spring-boot-starter-tomcat : 톰캣(웹서버)
    • spring-webmvc 스프링 웹 MV
  • spring-boot-starter-thymeleaf : 타임리프 템플릿 엔진
  • spring-boot-starter (공통)
    • spring-boot (스프링부트)
      • spring-core (스프링 코어)
    • spring-boot-starter-logging (로깅)
      • logback, slf4f

<테스트 라이브러리>

  • spring-boot-starter-test
    -junit : 테스트 프라임워크
    -mockito : 목 라이브러리
    -asserj : 테스트 코드 작성 편하게 도와주는 라이브러리
    -spring-test : 스프링 통합 테스트 지원

view 만들기

1. thymeleaf 템플릿 엔진

  • 브라우저에서 서버없이 로드할 수 있음
  • 서버 있으면 th안의 값, 없으면 html 태그안의 값이 표시됨
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<!-- th (thymeleaf) -->
    <p th:text="'안녕하세요, ' + ${data} + '님!'" />
</body>
</html>

서버 요청 처리 과정

  1. 웹 브라우저 요청 -> 내장 tomcat
  2. 요청 url이 Controller에 있는지 먼저 검사

controller에 없는 경우

  1. view 주소로 보고 viewResolver로 전달
  2. ViewResolver가 매핑해둔 html 주소를 thymeleaf 템플릿 엔진에 넘김
  3. thymeleaf 템플릿 엔진이 변환처리한 html을 브라우저에 넘김

controller에 있는 경우

  1. controller에서 처리해서 return

controller에서 브라우저로 데이터 보내는 방식

1. view 전달

@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(value = "name", required = false) String name, Model model) {
	model.addAttribute("name", name);
	return "hello-template"; // 마지막에 view 이름을 리턴
}

2. @requestBody - api방식

  • @requestBody는 http의 body에 문자내용 직접 반환

  • 객체 전달 시 HttpMessageConverter가 동작

  • 기본 문자 : StringHttpMessageConverter 동작

  • 객체 : MappingJackson2HttpMessageConverter (default)

  • api 방식에서 객체를 전달할 경우 json 형식으로 변환하여 데이터 전달

  1. java class로 정보 저장할 model 생성
  2. controller에서 인스턴스 만들어 데이터 저장
  3. 인스턴스 그대로 보내면 json 형식으로 전송됨
profile
문서화를 좋아하는 개발자

0개의 댓글