From bf87234827fba9c50ad43f1a1d88f3fc2b53c355 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sat, 19 Sep 2020 00:20:10 +0300 Subject: [PATCH] Move categories to global Spring Model Signed-off-by: Pekka Helenius --- .../fjordtek/bookstore/web/BookController.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index acd9ba2..e8d78a0 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -29,6 +29,12 @@ import com.fjordtek.bookstore.model.CategoryRepository; @Controller public class BookController { + @Autowired + private BookRepository bookRepository; + + @Autowired + private CategoryRepository categoryRepository; + private static final String landingPageView = "index"; private static final String bookListPageView = "booklist"; private static final String bookAddPageView = "bookadd"; @@ -47,17 +53,12 @@ public class BookController { private HttpServerLogger httpServerLogger = new HttpServerLogger(); - @Autowired - private BookRepository bookRepository; - - @Autowired - private CategoryRepository categoryRepository; - @ModelAttribute public void globalAttributes(Model dataModel) { // Security implications of adding these all controller-wide? dataModel.addAllAttributes(globalModelMap); + dataModel.addAttribute("categories", categoryRepository.findAll()); } ////////////////////////////// @@ -96,7 +97,6 @@ public class BookController { Book newBook = new Book(); newBook.setYear(Year.now().getValue()); dataModel.addAttribute("book", newBook); - dataModel.addAttribute("categories", categoryRepository.findAll()); httpServerLogger.log(requestData, responseData); @@ -166,7 +166,6 @@ public class BookController { Book book = bookRepository.findById(bookId).get(); dataModel.addAttribute("book", book); - dataModel.addAttribute("categories", categoryRepository.findAll()); httpServerLogger.log(requestData, responseData); @@ -193,7 +192,6 @@ public class BookController { ) { bookId = book.getId(); - dataModel.addAttribute("categories", categoryRepository.findAll()); if (bindingResult.hasErrors()) { responseData.setStatus(HttpServletResponse.SC_BAD_REQUEST);