I am creating a shopping cart application to learn Spring MVC. I want to display a list of all categories and subcategories in more than one page. i.e; Home Page, Products page, and Category page.
In the Home Page controller I am retrieving the list of categories, subcategories and featured products and passing them to the home page:
@Controller
public class CatalogController {
@Autowired
private CategoryConfigService categoryConfigurationService;
@Autowired
private ProductConfigService productConfigurationService;
private static final Logger logger = LoggerFactory
.getLogger(CatalogController.class);
/**
* Catalog Controller method which retrieves the information required in the
* application home page(Categories,SubCategories)
*
* @return Home Page View
*/
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String returnHomePage(Model model) {
logger.info("Processing information for home page");
List<Category> categoriesList = categoryConfigurationService
.getAllCategories();
Map<Category, List<SubCategory>> categoryMap = new HashMap<Category, List<SubCategory>>();
for (Category category : categoriesList) {
List<SubCategory> subCategoryList = categoryConfigurationService
.getAllSubCategoriesByCategoryId(category.getId());
categoryMap.put(category, subCategoryList);
}
model.addAttribute("categoryMap", categoryMap);
model.addAttribute("featProd",
productConfigurationService.getFeaturedProducts());
return "home";
}
}
What is the best way to store the object categoryMap
in Spring to access it in the header page?
The header page is common for all the JSPs in my web application. So I have used the application context scope to store and retrieve the categoryMap
.
What are the drawbacks of this approach? And are there any better ways of doing it?
I want to display the header page as shown in this image: