4

i have some question about Spring MVC annotation @ModelAttribute. In first method named as "addProduct" i create Model model and after call model.addAttribute i can use "product" name in jsp file,for example product.getProductPrice. But in second method named same as first,i added parameter " @ModelAttribute("product") Product product ",but why?? If i will delete this annotation, my program works as same as before,please explain me) Thank you very much,sorry for my English,i am from Ukraine)

@RequestMapping("/admin/productInventory/addProduct")
public String addProduct(Model model) {
    Product product = new Product();
    // add default for radio button!
    product.setProductCategory("Mobile Phone");
    product.setProductCondition("New");
    product.setProductStatus("active");

    model.addAttribute("product", product);

    return "addProduct";
}

@RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
public String addProduct(@ModelAttribute("product") Product product, HttpServletRequest request) {
    productDao.addProduct(product);

    MultipartFile productImage = product.getProductImage();
    String rootDirectory = request.getSession().getServletContext().getRealPath("/");
    System.out.println(rootDirectory);
    // product id as the file name
    // !!!! TODO
    // path = Paths.get(rootDirectory + "/WEB-INF/resources/image/" +
    // product.getProductId() + ".png");

    path = Paths.get("F:\\Spring\\eMusicStore\\src\\main\\webapp\\WEB-INF\\resources\\images\\"
            + product.getProductId() + ".png");

    if (productImage != null && !productImage.isEmpty()) {
        try {
            productImage.transferTo(new File(path.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Product image saving failed", e);
        }
    }
    return "redirect:/admin/productInventory";
}
2
  • It's not clear what you're asking. In the first case, you're creating the Product inside the controller and putting it in the model. In the second case, you're getting the Product information from the request contents. (And for heaven's sake, don't hard-code insane file manipulation like that! If you have to have something like that, use a property with @Value to specify it.) Commented Nov 6, 2016 at 23:20
  • Although it MIGHT appear to work the same in realtity it doesn't. Especially when starting to use validation or combined with @SessionAttributes you will see weird behavior. Commented Nov 7, 2016 at 7:58

1 Answer 1

2

Purpose @ModelAttribute is bind param/properties from request a model object, say @ModelAttribute("person") Person person in your method, it will bind properties from object such name, age to Person and construct a object out of it. It does not pass anything to your view, it job finishes once the request submitted. Not carried down to the view of that action.

In contrast, when you have Model model you are explicitly constructing an object with property added to its attribute. It will be carried down to your view unlike what @ModelAttribute does above

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.