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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

代码玩家   2021-06-18 我要评论
想了解springboot+thymeleaf找不到视图的解决方案的相关内容吗,代码玩家在本文为您仔细讲解springboot thymeleaf找不到视图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们