springboot+thymeleaf找不到视图的解决方案

网友投稿 1299 2023-01-08

springboot+thymeleaf找不到视图的解决方案

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配置:

UdBXnZKQsspring:

thymeleaf:

cache: false #开发时关闭缓存,http://不然没法看到实时页面

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业app开发制作多少钱(企业app开发制作多少钱)
下一篇:小程序生态社区服务(微信小程序社区服务平台)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~