5. BookController 클래스에 도서 정보를 수정하는 GET 방식의 getUpdateBookForm() 메서드와 POST 방식의 submitUpdateBookForm() 메서드를 각각 추가합니다.
코드 17-26 BookController.java
package com.springmvc.controller;
...
@Controller
@RequestMapping("/books")
public class BookController {
...
➊
@GetMapping("/update")
public String getUpdateBookForm(@ModelAttribute("updateBook") Book book, @RequestParam("id") String bookId, Model model) {
Book bookById = bookService.getBookById(bookId);
model.addAttribute("book", bookById);
return "updateForm";
}
➋
@PostMapping("/update")
public String submitUpdateBookForm(@ModelAttribute("updateBook") Book book) {
MultipartFile bookImage = book.getBookImage();
String rootDirectory = "c:/upload/";
if (bookImage != null && !bookImage.isEmpty()) {
try {
String fname = bookImage.getOriginalFilename();
bookImage.transferTo(new File("c:/upload/" + fname));
book.setFileName(fname);
} catch (Exception e) {
throw new RuntimeException("Book Image saving failed", e);
}
}
bookService.setUpdateBook(book);
return "redirect:/books";
}
}