【发布时间】:2018-07-06 21:32:04
【问题描述】:
我是 Spring MVC 的新手。当我更改以下代码时:
@RequestMapping("/showform")
public String showForm(Model theModel) {
Student student = new Student();
theModel.addAttribute("student", student);
return "student-form";
}
以下代码:
@RequestMapping("/showform")
public String showForm() {
Model theModel;
Student student = new Student();
theModel.addAttribute("student", student);
return "student-form";
}
我收到此错误:局部变量 theModel 可能尚未初始化。
我的问题是Model theModel 怎么会首先被初始化(作为方法参数)?
【问题讨论】:
-
调用
showForm方法的任何东西(Spring MVC 基础架构)都已初始化并传入Model参数。 -
Spring 很神奇,它根据您使用的注解从参数中注入依赖项。所以第一个有效,因为春天给了你价值。
标签: java spring-mvc