본문 바로가기
Backend - Java Spring/SB 입문_Apple(완)

[Co_App] Spring Basic 복습

by VictorMeredith 2024. 8. 29.

1. Controller

@ResponseBody 넣으면 저렇게 보냄(RestAPI)

 

2. 타임리프 설치하면

- 엔진때문에 static이 아닌 template에서 찾는다.

3. ThymeLeaf MVC 문법

Model model -> addAttribute 호출

 

th:text="${문법}"

 

3. JPA

- build.gradle

 

- application.properties

 

- Entity

 

- Repository 만들기

 

- repository 가져다가 사용하기

 

4. Thymeleaf

- thymeleaf 반복문 th:each

 

5. post

MVC에서 Controller 레이어에서 redirect 하는 방법

 

- Map 자료형

 

- @ModelAttribute 이런 것도 있음

 

- thymeleaf의 Fragment 문법(컴포넌트)

 

- thymeleaf Fragment 문법 가져다쓰기

 

6. Detail페이지

- ifPresent, Lambda를 통해 깔끔하게 작성 가능

@PathVariable 쓰면됨

- Optional 사용해도 됨

- item.isPresent() 로 if분기해도 됨

 

- Thymeleaf th:href 문법

 

7. 예외처리(RestAPI)

- Thymeleaf 에서는 template 폴더 밑에 error.html 만 만들어놔도 에러나면 그거 보내줌

- status 등 다양한 변수 사용할 수 있으니 찾아서 쓰셈

- 근데 RestAPI에서는 처리 해줘야함

 

1. try-catch 문으로 하기

Express에서 썼던건데 여기서도 사용하려나

 

2. Controller 마다 @ExceptionHandler 붙이기

 

3. 모든 Controller 파일의 에러를 캐치하기 (@ControllerAdvice)

- 한 파일에만 몰아넣으면 됨

- 여기서는 MyExceptionHandler class를 만들어서 여기서 함

이렇게 클래스를 다르게 하면 그 에러가 나는 경우에 글로벌로 처리하게 됨

 

8. Layer (Service, Controller, DAO)

@RequiredArgsConstructor -> private final 불러올거 : 생성자 주입

 

9. 수정 기능

@Transactional 이 있어야 Dirty Check 이 발동된다.

 

10. ifPresentOrElse

lambda

- .ifPresentOrElse(item -> 있으면 할거, () -> 없으면 할거)

- 보편적인 처리 방법은 ifPresent, map을 더 많이 씀

 

11. Controller의 메서드 내 파라미터 

- @PathVariable : 경로 변수

- @RequestParam : URL 쿼리스트링(?a=b& or form 데이터 변수)

- @RequestBody : 바디 변수 

 

댓글