What is @ModelAttribute in Spring MVC?

后端 未结 13 851
一个人的身影
一个人的身影 2020-11-22 08:43

What is the purpose and usage of @ModelAttribute in Spring MVC?

13条回答
  •  隐瞒了意图╮
    2020-11-22 09:40

    At the Method Level

    1.When the annotation is used at the method level it indicates the purpose of that method is to add one or more model attributes

    @ModelAttribute
    public void addAttributes(Model model) {
    model.addAttribute("india", "india");
    }
    

    At the Method Argument 1. When used as a method argument, it indicates the argument should be retrieved from the model. When not present and should be first instantiated and then added to the model and once present in the model, the arguments fields should be populated from all request parameters that have matching names So, it binds the form data with a bean.

     @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
      public String submit(@ModelAttribute("employee") Employee employee) {
      return "employeeView";
      }
    

提交回复
热议问题