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

[ Page클래스 ]

id는 자동생성(AI),  어노테이션은 두가지를 쓴다

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

 

IDENTITY 와 AUTO의 차이점 
IDENTITY - DB에서 쓴 id를 가져옴 그래서 각 테이블마다 1로 시작 가능
AUTO - A테이블 1234,  B테이블 567

 

 


테이블과 이름이 다를경우 어노테이션

@Entity
@Table(name = "pages") 

 

 

 

 

열과 이름이 다를경우 어노테이션

@Column(name="username")

 

 

반응형
LIST

부트스트랩 css  /  js 추가하기

 

css는 <head>안에 추가

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">

 

 

js는 body끝나는 지점에 추가

 <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" ></script>
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>

 

 

 

 


 

static/css/style.css 추가 하는 법 

th:href="@{/}" /까지가 8080까지고

/css/style.css 는 static폴더 안의 폴더를 넣는다

<link rel="stylesheet" th:href="@{/css/style.css}">  

 

 

반응형
LIST
***************************
APPLICATION FAILED TO START
***************************


Description:


Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.


Reason: Failed to determine a suitable driver class 


DB설정하라는 에러메세지이다.

 

 

 

/src/main/resources/application.properties 파일

#MySQL DB설정
spring.datasource.url=jdbc:mysql://localhost:3306/uglyMarket?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234

DB설정해주고  서버 실행하기

 

 

 

 

http://localhost:8080/ 

브라우저를 열면 잘 뜬다.

반응형
LIST

+ Recent posts