KZc*_*ing 3 model-view-controller spring spring-mvc thymeleaf spring-boot
我首先gradle bootRun使用以下控制器类成功构建了我的 Spring MVC 项目:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
public String hello() {
return "resultPage";
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将其更改为将数据传递给我的视图类:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
public String hello(Model model) {
model.addAttribute("message", "Hello from the controller");
return "resultPage";
}
}
Run Code Online (Sandbox Code Playgroud)
当我现在构建我的项目时,出现以下错误:
HelloController.java:13: error: cannot find symbol
public String hello(Model model) {
^
symbol: class Model
location: class HelloController
1 error
:compileJava FAILED
FAILURE: Build failed with an exception.
Run Code Online (Sandbox Code Playgroud)
任何想法我做错了什么?
我解决了这个问题。
如果我们希望 DispatcherServlet 将 Model 注入到函数中,我们应该做的一件事就是导入 Model 类。
import org.springframework.ui.Model;
Run Code Online (Sandbox Code Playgroud)
所以,我将我的控制器类更改为以下内容并且它起作用了!
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
@Controller
public class HelloController {
@RequestMapping("/")
public String hello(Model model) {
model.addAttribute("message", "Hello from the controller");
return "resultPage";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12991 次 |
| 最近记录: |