model.addAttribute() parameters

I’m new to Spring MVC Framework. I’m doing some self study to extend my knowledge in Java.

This is how I understand the getProducts() code definition from a tutorial I’m following but please correct me if I’m wrong.

Controller requests something from the Data Access Object > Data Access Object gets the data from a Database or a Model through the getProductList() method > Stores the information to list > Then binds the list to the model.

So I got two question about this.

Is the inclusion of model as parameter in public String getProducts(Model model) considered the dependency injection

Is products (within quotes) in model.addAttribute("products",products); just a name which I can change to whatever I like or should it match something?

public class HomeController {

    private ProductDao productDao = new ProductDao();

    @RequestMapping("/")
    public String home(){
        return "home";
    }

    @RequestMapping("/productList")
    public String getProducts(Model model){
        List<Product> products = productDao.getProductList();
        model.addAttribute("products",products);

        return "productList";  //productList string is the productList.jsp which is a view
    }

    @RequestMapping("/productList/viewProduct")
    public String viewProduct(){
        return "viewProduct";
    }
}

I’d appreciate any explanation or comment.

Thank you.

Leave a Comment