有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java理解Spring引导中的@ModelAttribute和@RequestMapping

代码示例取自此处:https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation

以代码为例:

    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public String submit(
      @ModelAttribute("employee") Employee employee,
      BindingResult result, ModelMap model) {
        if (result.hasErrors()) {
            return "error";
        }
        model.addAttribute("name", employee.getName());
        model.addAttribute("id", employee.getId());

        employeeMap.put(employee.getId(), employee);

        return "employeeView";
    }

我知道@RequestMapping只是将端点映射到此方法,但我不了解的是:

  1. 调用方法时,方法参数“来自”哪里?我不知道ModelMap来自哪里

  2. 它如何知道@ModelAttribute("employee")是什么?这是来自View(HTML),对吗

  3. Model到底是什么?它只是<String, Object>的映射类型吗?这个Model的寿命是多少


共 (0) 个答案