解决SpringBoot项目通过Controller的return没法跳转页面的问题

本文详细记录了解决SpringBoot项目中Controller跳转页面时出现的模板解析错误问题,通过调整pom.xml中的资源过滤设置,成功实现了页面的正常跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers

这个是我的报错。

我在SpringBoot项目中的Controller中通过return跳转页面的时候无法跳转,项目报上面的异常。首先我把Controller类中的注解@RestController修改为了@Contrller,但是没有什么作用,接着我在Controller类中的方法上面添加了注解@Responsebody,这个时候发现页面还是没有跳转成功,但是页面直接打印出来我返回的内容。随后我检查了自己的项目目录,关键是templates的位置,对比了我的配置文件application.yml中关于thymeleaf的配置。

仔细对照,并没有什么不对的地方。然后我查看了添加的依赖,我的依赖是

<dependency>       

    <groupId>org.springframework.boot</groupId>      

    <artifactId>spring-boot-starter-thymeleaf</artifactId>    

</dependency>

依赖也没有问题。最后我注意到我的pom.xml中的build中有一段配置

这段配置是为了项目能够扫描到xml或者priperties而配置的,其实并没有什么额外的作用,但是也就是这段配置导致找不到我的html页面,将他们注释掉就好了,或者在里面添加上html文件类型就好,通过Controller中的return就可以正常跳转页面了。

Controller类中的跳转方法:

@RequestMapping(value = "/index")
@ResponseBody
public ModelAndView index(Model model){
    model.addAttribute("boot_name","jack");
    model.addAttribute("boot_age",20);
    model.addAttribute("boot_info","jack,success");
    return new ModelAndView("index");
}

添加html类型如下:

<!--保证配置文件能被扫描到-->
<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
      <include>**/*.html</include>
    </includes>
  </resource>
</resources>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one_smail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值