springboot使用freemarker报错:Circular view path: would dispatch back to the current handler URL again

在springboot中使用freemarker模板引擎实现一个小例子
在这里插入图片描述
在浏览器中访问报错:
在这里插入图片描述
服务端的错误信息为:

javax.servlet.ServletException: Circular view path [user]: would dispatch back to the current handler URL [/user] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

错误原因:没有找到视图资源
排查:

  1. 创建的模板文件是否在template目录下。
  2. 模板文件创建是否异常

查看源码(idea中可以连续点击两下shift键):
FreeMarkerAutoConfiguration.class ->FreeMarkerProperties.class

在这里插入图片描述
从源码中可以看到模板文件的后缀默认为".tflh",而我的模板文件后缀为“.tfl”。修改后缀问题解决。
在这里插入图片描述如果我们想要自定义freemarker模板的后缀或者模板的位置可以在application.properties中配置。

# 自定义Freemarker模板位置,模板的默认位置在classpath下面的template目录中
spring.freemarker.template-loader-path=classpath:/bj
# 配置模板后缀
spring.freemarker.suffix=.ftl

猜你喜欢

转载自blog.csdn.net/weixin_43398820/article/details/107776038