You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:40:27