SpringMVC无法正常访问使用spring配置部分的内容,怎么办?

历经两天,查看了网上各种的解决方案,终于修改成功!

1.第一当然是要确保自己的代码一定一定要正确,URL地址、Bean的注册(例:

@ComponentScan("com.example.controller") 

)等,最好检查到“Ctrl”+左键点击可以定位到正确的地方。

2.依赖配置与版本适配

本人用的是Spring6 和 Tomcat10 和 JDK17(因为Spring6硬性要求要搞版本的JDK和Tomcat)

一开始我的依赖spring-webmvc是5的版本

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.x.x</version>
</dependency>

然后既不报错,运行也成功,只不过spring配置部分直接失效,完全不需要此部分,而且只用index.html可以运行,换个名字都不行!!!

这是我的配置部分:

@Controller
public class HelloController {
    @RequestMapping ("/")
    public ModelAndView index(){
        return new ModelAndView("index");
    }
}
很简单的一个配置类,就是不行。

但是,我把依赖的版本好升级到6.0.0后,就可以了!!!

以下是我SpringMVC的基本依赖:

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring6</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.37.android4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2-extension-spring6</artifactId>
            <version>2.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>2.1.1</version>
        </dependency>


        <!--        若出现  无法访问javax.servlet.ServletException-->
        <!--        找不到javax.servlet.ServletException的类文件   的错误-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
        </dependency>
    </dependencies>

希望各位不要遇到我这个问题~因为真的很痛苦......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值