springboot+thymeleaf找不到视图的解决方案
springboot+thymeleaf找不到视图
情况:
springboot + thymeleaf打成jar包后,报错,但在eclipse本地跑却可以:
template might not exist or might not be accessible by any of the configured Template Resolvers
yml配置:
spring: thymeleaf: cache: false #开发时关闭缓存,不然没法看到实时页面 mode: HTML5 # 用非严格的 HTML #enabled: true encoding: UTF-8 prefix: classpath:/templates/ suffix: .html servlet: content-type: text/html
controller返回视图:
@RequestMapping("demo") public String demo(Model model) { //return "/demo";//这种是有问题的 return "demo"; }
解释:
这里其实是由于我们的yml配置中,已经配置了/templates/,因此如果返回/demo的话,那就会找不到,因为映射视图变成了://demo,所以,这里最好去掉其中一个“/”;
不然打成jar包后,会找不到,这个要作为项目的规范,不然后面发布正式时,太多也不好修改;如果有更好的办法也请告诉我一声,谢谢。
springboot 使用thymeleaf模板遇到的一些问题
使用springboot+thymeleaf遇到一些问题,主要归为如下几点:
1.在/templates目录下创建自定义目录/my,并在该目录下创建index.html,程序中如何访问index.html
2.如果不使用/templates目录作为默认路径,该如何配置
问题1
解决方式:
在controller层方法中通过设置ModelAndView名称的为:my/index,然后返回该ModelAndView,然后该接口方法时就会跳转到index.html
示例代码如下:
@RequestMapping(value="getIndex") public ModelAndView getIndex(ModelAndView model)throws Exception { //访问自定义目录下/templates/my/index.html,要注意路径格式 model.setViewName("my/index"); return model; }
问题2
解决方式:
在application.properties配置文件中通过spring.thymeleaf.prefix属性进行设置,例如设置默认路径为/templates/my
示例代码如下:
spring.thymeleaf.prefix=classpath:/templates/my
springboot+thymeleaf使用的代码如下:
https://github.com/ingorewho/springboot-develope/tree/master/springboot-thymeleaf
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
springboot yml定义属性,下文中${} 引用说明
这篇文章主要介绍了springboot yml定义属性,下文中${} 引用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-04-04SpringBoot2.X Kotlin系列之数据校验和异常处理详解
这篇文章主要介绍了SpringBoot 2.X Kotlin系列之数据校验和异常处理详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-04-04
最新评论