springboot+thymeleaf 跳转修改默认页面路径及相关配置

 

 

 

一般来说springboot默认静态资源就是resources-》static目录下,但是总有各种各样的原因,所以还是直接配置

一下比较好。

一般来说被thymeleaf修饰的页面一般是thymeleaf默认在resources-》templates目录下作为前缀,表示被thymeleaf修饰的页面在这里作为根目录。(注意:不是静态资源)

但是还是希望大家能加上对应的配置,避免各种各样的尴尬。

application.yml我的配置如下:

spring:
    mvc:
          static-path-pattern: /**
    resources:
          static-locations: classpath:/static/
    thymeleaf:
          prefix: classpath:/templates/
          suffix: .html
          cache: false #关闭缓存

 

如果你的是application.properties那么对应的就是

spring.mvc.static-path-pattern=/**表示所有的访问都经过静态资源路径;

spring.resources.static-locations=classpath:/static/在这里表示配置静态资源路径

Thymeleaf.prefix=classpath:/templates/ 在这里作为前缀,表示被thymeleaf修饰的页面在这里作为根目录。(注意:不是静态资源)

Thymeleaf.suffix=.html 在这里表示以.html作为后缀。

请根据你的需要进行配置。

 

给大家参考一下页面中导入各种各样的外部资源应该怎么操作。

我的目录结构因为是这样:

 

所以导入如下:

导入css:

<link rel="stylesheet" th:href="@{/index/css/owl.theme.css}">

 

导入js:

<script th:src="@{/index/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js}"></script>

 

导入图片:

<img th:src="@{/index/img/logo.png}" alt="Logo">

 

链接网页:

除非你把网页放到静态资源中去,不然还是要通过controller进行跳转。

<li><a th:href="@{/system/login}">- 个人主页</a></li>

 

 

有一些地方需要注意一下:

@{}表示的是调用静态资源那一块,也就是你设置的spring.resources.static-locations=classpath:/static/这里面的内容

不是这里面的内容是获取不到的。

 

@{/index/img/slider/image_1.jpg}

说明一下,@{/.....}里面的/的作用是从你设置的application.yml中

resources:
      static-locations: classpath:/templates/ (意思是http://localhost:8080从这里开始)

开始的,如:

 http://localhost:8080/index/img/slider/image_1.jpg(正确)

如果不加上就是从你调用的controller层类上面那个映射开始,如

@RequestMapping("/system")
public class SystemController {

........

}

system开始.如:

 http://localhost:8080/system/index/img/slider/image_1.jpg(错误)

所以根据你项目结构而加以区别。

一般来说请你加上/

 

 

最重要的一点,请不要页面上有th:href=”” 或者th:src=””等带有th:标签然后后面不接任何东西的代码。

这样编辑时不会报错,但是运行之后会报错,而且看了你自己都蒙。找问题根源浪费你大量时间。错误如下:

16:07:39.100 [http-nio-8080-exec-1] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8080-exec-1] Exception processing template "index/index": An error happened during template parsing (template: "class path resource [templates/index/index.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index/index.html]")

 

如果要写的话请th:href=@{}或者th:src=@{}这样写。其他还没有验证,自行体验。

对于想要熟悉基本的thymeleaf标签的孩子们,可以来这个链接看一下,个人觉得还不错。

https://www.cnblogs.com/hjwublog/p/5051732.html

 

 

猜你喜欢

转载自blog.csdn.net/boywcx/article/details/81362104