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>