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

 

 

  • 15
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot是一个开源的Java开发框架,用于快速构建基于Spring的应用程序。Thymeleaf是一个Java的服务器端模板引擎,可以与Spring Boot框架很好地集成。 要实现HTML页面之间的,可以使用Thymeleaf的语法结合Spring Boot的控制器来完成。 首先,需要在pom.xml文件中添加Thymeleaf的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 接下来,在Spring Boot的控制器中添加一个处理请求的方法,该方法返回要的HTML页面的名称: ```java @Controller public class MyController { @RequestMapping("/hello") public String hello() { return "hello"; // 这里返回的是要的HTML页面的名称,不需要后缀名 } } ``` 然后,在resources/templates目录下创建名为hello.html的HTML页面,用于展示hello页面的内容: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <h1>Hello, Thymeleaf!</h1> </body> </html> ``` 最后,启动Spring Boot应用程序,在浏览器中访问http://localhost:8080/hello,即可看到到hello.html页面,并显示"Hello, Thymeleaf!"的信息。 以上就是使用Spring Boot和Thymeleaf实现HTML页面的简单示例。通过Thymeleaf的语法和Spring Boot的控制器,我们可以方便地实现页面之间的和数据的渲染。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值