스프링부트, 타임리프(Thymeleaf)로 파일게시판 만들기
#5 Controller 작성
html 페이지에서 요청받아 처리할 Controller를 작성합니다. 코드 내용이 생각보다 길기 때문에 오타에 주의하세요. Controller 패키지 내부에 작성합니다.
FileBoardController.java
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.bean.FileBoardVO;
import com.example.demo.service.FileBoardService;
@Controller
@RequestMapping("/fileBoard")
public class FileBoardController {
@Autowired
FileBoardService fboardService;
@RequestMapping("/list")
private String fileBoardList(Model model, HttpServletRequest request) {
List<FileBoardVO> testList = new ArrayList<>();
testList = fboardService.getFileBoardList();
model.addAttribute("testlist", testList);
return "/fileBoard/list";
}
@RequestMapping("/detail/{b_no}")
private String fileBoardDetail(@PathVariable("b_no") int b_no, Model model) {
model.addAttribute("detail", fboardService.fileBoardDetail(b_no));
return "fileBoard/detail";
}
@RequestMapping("/insert")
private String fileBoardInsertForm(@ModelAttribute FileBoardVO board) {
return "fileBoard/insert";
}
@RequestMapping("/insertProc")
private String fileBoardInsertProc(@ModelAttribute FileBoardVO board, HttpServletRequest request) {
fboardService.fileBoardInsert(board);
return "forward:/fileBoard/list"; //객체 재사용
}
@RequestMapping("/update/{b_no}")
private String fileBoardUpdateForm(@PathVariable("b_no") int b_no, Model model) {
model.addAttribute("detail", fboardService.fileBoardDetail(b_no));
return "fileBoard/update";
}
@RequestMapping("/updateProc")
private String fileBoardUpdateProc(@ModelAttribute FileBoardVO board) {
fboardService.fileBoardUpdate(board);
int bno = board.getB_no();
String b_no = Integer.toString(bno);
return "redirect:/fileBoard/detail/"+b_no;
}
@RequestMapping("/delete/{b_no}")
private String fileBoardDelete(@PathVariable("b_no") int b_no) {
fboardService.fileBoardDelete(b_no);
return "redirect:/fileBoard/list";
}
}
잘 작성하셨나요? 이제 요청을 받아줄 Controller를 작성했으니 다음 글에서 요청을 전달할 html페이지를 작성하겠습니다.
댓글