build.gradle
implementation 'org.springframework.boot:spring-boot-starter-validation'
유효성 검사를 위해 추가
지난번 @NotEmpty를 쓰기 위하여 추가
BoardDto.java
package jpa.board.dto;
import com.querydsl.core.annotations.QueryProjection;
import jpa.board.entity.Board;
import jpa.board.entity.Member;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
@Data
public class BoardDto {
private Long id; //시퀀스
@NotEmpty(message = "제목은 필수입니다.")
private String title; //제목
private String content; //내용
private LocalDateTime regDate; //등록 날짜
private LocalDateTime uptDate; //수정 날짜
private Long viewCount; //조회수
private String username; //사용자 이름
public BoardDto() {
}
public BoardDto(String title, String content){
this.title = title;
this.content = content;
}
@QueryProjection
public BoardDto(Long id, String title, String content, LocalDateTime regDate , LocalDateTime uptDate, Long viewCount, String username) {
this.id = id;
this.title = title;
this.content = content;
this.regDate = regDate;
this.uptDate = uptDate;
this.viewCount = viewCount;
this.username = username;
}
public Board toEntity(Member member){
return Board.builder()
.member(member)
.title(title)
.content(content)
.build();
}
}
추가 변경사항 없음
@NotEmpty를 확인
BoardController.java
package jpa.board.controller;
import jpa.board.dto.BoardDto;
import jpa.board.repository.CustomBoardRepository;
import jpa.board.service.BoardService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class BoardController {
private final CustomBoardRepository customBoardRepository;
private final BoardService boardService;
@GetMapping("/")
public String list(String searchVal, Pageable pageable, Model model) {
Page<BoardDto> results = customBoardRepository.selectBoardList(searchVal, pageable);
model.addAttribute("list", results);
model.addAttribute("maxPage", 5);
model.addAttribute("searchVal", searchVal);
pageModelPut(results, model);
return "board/list";
}
private void pageModelPut(Page<BoardDto> results, Model model) {
model.addAttribute("totalCount", results.getTotalElements());
model.addAttribute("size", results.getPageable().getPageSize());
model.addAttribute("number", results.getPageable().getPageNumber());
}
@GetMapping("/write")
public String write(Model model) {
model.addAttribute("boardDto", new BoardDto());
return "board/write";
}
@PostMapping("/write")
public String save(@Valid BoardDto boardDto, BindingResult result) {
//유효성검사 걸릴 시
if (result.hasErrors()) {
return "board/write";
}
boardService.saveBoard(boardDto);
return "redirect:/";
}
@GetMapping("/update")
public String upadte() {
return "board/update";
}
@PostMapping("/delete")
public String delete(@RequestParam List<String> boardIds) {
for(int i = 0; i < boardIds.size(); i++) {
Long id = Long.valueOf(boardIds.get(i));
boardService.deleteBoard(id);
}
return "redirect:/";
}
}
write와 save 메소드 수정
wrtie.html
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/default_layout}">
<head layout:fragment="css">
<style>
.fieldError {
border-color: #bd2130;
}
.form-group p {
color: red;
}
</style>
</head>
<div layout:fragment="content" class="content">
<form th:action th:object="${boardDto}" method="post">
<article>
<div class="container" role="main">
<div class="form-group">
<label for="title">제목</label>
<input type="text" class="form-control" id="title" name="title" placeholder="제목을 입력해 주세요" th:class="${#fields.hasErrors('title')}? 'form-control fieldError' : 'form-control'">
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Incorrect date</p>
</div>
<br>
<div class="mb-3">
<label for="reg_id">작성자</label>
<input type="text" class="form-control" id="reg_id" name="regId" value="관리자" readonly>
</div>
<br>
<div class="mb-3">
<label for="content">내용</label>
<textarea class="form-control" rows="5" id="content" name="content" placeholder="내용을 입력해 주세요"></textarea>
</div>
<br>
<br>
<div>
<button type="submit" class="btn btn-sm btn-primary" id="btnSave">저장</button>
<button onclick="location.href='/'" type="button" class="btn btn-sm btn-primary" id="btnList">목록</button>
</div>
</div>
</article>
</form>
</div>
</html>
<script>
</script>
유효성 검사 체크 결과 표시