from submit을 할때 th:object="${project}" →  project객체를 받는다.

 

 

전체에러

<div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">에러 발생</div>
 
 
 
유효성검사
<span class="error" th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></span>

 

 

<form enctype="multipart>

:모든 문자를 인코딩하지 않음을 명시함.
이 방식은 <form> 요소가 파일이나 이미지를 서버로 전송할 때 주로 사용함.

martipart : 파일이나 이미지를 서버로 전송할 때 주로 사용함.

 

 

유효성 검사

- 가격에는 어노테이션을

@Pattern(regexp = "^[1-9][0-9]*")

^시작할때 [1-9] 1에서9까지 쓸수있다 [0-9]뒷부분은 0에서 9까지 = 1~9999999까지
 
 
 
 

updatable = false 

처음 생성할때는 업데이트가 안되도록
@Column(name = "created_at", updatable = false) 
@CreationTimestamp   //생성될 때마다 자동생성(insert)
private LocalDateTime createAt;
 
 
 

 

카테고리 반복문

th:each="category : ${categories}"

<div class="form-group">
  <label for="">카테고리</label>
  <select class="form-control" th:field="*{categoId}" >
    <option th:each="category : ${categories}" th:text="${category.name}"></option>
  </select>
  <span class="error" th:if="${#fields.hasErrors('categoryId')}" th:errors="*{categoryId}"></span>
</div>

 

get매핑

카테고리 리스트가 나오도록 

//카테고리 선택하도록
List<Category> categories = categoryRepo.findAll();
model.addAttribute("categories", categories);

 


오류발생

 

카테고리 옵션에 value 값을 줘야한다?

 

th:value="${category.id}" 

<option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.name}"></option>

 

결과 

value값에 id를 넣어서 카테고리를 찾아넣어준다.

카테고리 선택시 전체는 빼준다.
<option th:if="${category.name} != '전체'" th:each="category : ${categories}" th:value="${category.id}" th:text="${category.name}"></option>
 
 
 
 
 
 
#
select에 th:field="*{categoryId}" 는 왜 필요할까??

 

반응형
LIST

+ Recent posts