Shopping cart application in Spring MVC 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&lt;Category&gt; categoriesList = categoryConfigurationService
                .getAllCategories();
        Map&lt;Category, List&lt;SubCategory&gt;&gt; categoryMap = new HashMap&lt;Category, List&lt;SubCategory&gt;&gt;();
        for (Category category : categoriesList) {
            List&lt;SubCategory&gt; 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:
I think you need different approach to this. It's good practice when you don't have much of business logic in your controller. Maybe you should try creating a different class that will fill pages with content, based on something, like a template. 

Now answering to your question: 

You can create method with annotation @PostConstruct, which Spring will execute after done with his setup. In this method initialize field that you need, or in your case set application scope attribute with data that you need. 

Also, I don't know if you knew it, you can create jpsf - jsp parts, that you can include in any of your jsp. As an example, you can create part with menu, CSS link or other parts, that share all pages, and just include it in your pages - makes jps look much more cleaner.

Here how u can bind objects to any response of your controller: 

@ModelAttribute(value = "foo") 
public String addAttribute(){
return "string";
}


Or:

@ModelAttribute()
public void addAttribute(Model model){
   model.addAttribute("roo", "one");
   model.addAttribute("loo", "two");
   model.addAttribute("moo", "some");
}


this how you bind for every response of your application: 

@ControllerAdvice
public final class GlobalController {
    @ModelAttribute
    public void addAttributes(Model model) {
       model.addAttribute("msg", "Welcome to My Country");
    }
}