java - 了解 Spring Boot 中的 @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
只是将端点映射到此方法,但是我不明白的是:
调用方法时,方法参数“来自”哪里?我不知道从哪里来
ModelMap
。它怎么知道
@ModelAttribute("employee")
是什么?这是来自View
(HTML),对吧?什么是
Model
真的?它只是地图的类型<String, Object>
吗?的寿命是Model
多少?
解决方案
推荐阅读
- swift - 如何将多个 UI 分配给一个参数,例如 UILabels 和 UIButtons?
- google-apps-script - 非重复电子邮件 Google 表格脚本 - 运行脚本后防止发送重复电子邮件
- linux - 从 Linux 服务器调用 Windows 服务器上的 VBScript
- javascript - promise.resolve() 和 reject() 中的可接受参数
- ruby-on-rails - 如何在 simple_calendar 上输出任务标题
- node.js - 您如何使用节点 posgres 进行动态插入查询?
- php - password_verify 始终返回 false
- python - 如何返回列表中匹配的所有匹配项?
- r - 使用混合悬停和单击在闪亮的应用程序中创建横断线
- javascript - 当页脚出现时重新定位滚动到顶部按钮(所以它永远不会重叠)