Spring Boot中Model.addAttribute方法无法解析问题求助
解决Spring Boot中Cannot resolve method 'addAttribute' in 'Model'错误
问题根源
你的代码里导入了错误的Model类:
import ch.qos.logback.core.model.Model;
这个类属于logback日志框架,没有addAttribute方法。Spring MVC控制器中用来传递数据到视图的正确Model类是org.springframework.ui.Model。
修复步骤
- 替换导入语句:将错误的Model导入替换为Spring官方的类
import org.springframework.ui.Model; - 刷新Maven依赖:在STS中右键项目 → 选择
Maven→Update Project,确保依赖正确加载 - 重启STS:让IDE重新识别类结构,消除错误提示
修正后的userController代码
package com.example.projet.user; import org.springframework.ui.Model; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @Controller public class userController { @Autowired private userService service; @GetMapping("/users") public String showUserList(Model model){ List<user> ListUsers = service.listAll(); model.addAttribute("ListUsers",ListUsers); return "users"; } }
关于pom.xml的说明
你的pom.xml配置没有问题,spring-boot-starter-web和spring-boot-starter-thymeleaf依赖已经包含了Spring MVC所需的Model类,不需要额外添加依赖或插件。
内容的提问来源于stack exchange,提问作者Alaa Amari
相关产品推荐
相关产品推荐

