pom.xml 에서 Data JPA 설치 해야함.

JpaRepository<>

리파지토리에 crud처리를 위한 공통 인터페이스

ex) delete, deleteAll, deleteAll, deleteById, existsById, findById, save ..

 

* CrudRepository : CRUD 관련 기능들을 제공

CrudRepository 보다 업그레이드 된거라 볼 수 있다.

 

 


@ModelAttribute 은  Model model , addAttibute 안 쓰고 간략하게 쓸 수 있다.

 

예를들어 

@GetMapping("/add")
public String add(Model model) {
   model.addAttribute("page",new Page());
   return "/admin/pages/add";
}

@ModelAttribute 을 써서 아래처럼 간략하게 만든다

@GetMapping("/add")
public String add(@ModelAttribute Page page) { 
   return "admin/pages/add";
}

 

버튼을 눌렀을때

th:action 

th:href

차이는??

 

 

유효성검사 

@Validation

 

모든 에러가 발생할때
<div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">에러 발생</div>
 
각 에러가 발생할때
<span class="error" th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></span>

 

BindingResult : page에 유효성검사해서 에러가 나면 결과가 나오도록 하는

html에 에러 설정하기

전체: <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">에러 발생!</div>
제목: <span class="error" th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></span>

 

 

 

☆ 쉬운데 실수하기 쉬운 부분 

form을 post로 보내고 버튼을 submit으로 전송해줘야함.

<form method="post">

<button type="submit"> 

그래야 에러메세지가 넘어감

 


 

attr.addFlashAttribute("message", "페이지 추가되었습니다.");

attr.addFlashAttribute("alertClass", "alert-success");

 

[html]
<div th:if="${message}" th:class="${'alert ' +  alertClass}" th:text="${message}"></div> 
 
th:class 는 < class = "alert alert-success" > 이다.
 
 
attr.addFlashAttribute("alertClass", "alert-success");
→ 클래스 이름이 alert 이면  alert-success 의 통과알림? 의 초록색 창 뜨기
attr.addFlashAttribute("message", "페이지 추가되었습니다.");
→ th:if="${message}"  message 가 있다면 th:text 로 "페이지가 추가되었습니다." 

 

 

 

 

 

※ 둘의 차이

 

어떠한 값으로도 초기화하지 않은것.

String slug = (page.getSlug() == null) 

 

공백일때.

String slug = (page.getSlug() == "")   

반응형
LIST

+ Recent posts