springboot项目thymeleaf排雷

1、当我们想把前端页面加入到项目中时,springboot提供了一种前端页面模板,我们在resources文件夹下新建templates文件夹,将HTML5文件写入。
2、在做这个之前,我们需要把thymeleaf的相关依赖加入compile(‘org.springframework.boot:spring-boot-starter-thymeleaf’)我用的gradle,加入相关包即可。
3、然后在配置文件里面加入配置项

#thymeleaf
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false

这里要格外注意spring.thymeleaf.prefix=classpath:/templates/配置项,最好可以粘贴,自己写可能大致路径有问题,导致访问不到页面。
4、注意些return的时候只要写templates文件夹下面的路径就好了,不需要再多写,多写的话,会找不到那个html文件,还有不要在HTML前面加/,对于路径要求非常严格,写错就访问不到,还有就是不要写@RestController,请求接口时会直接返回字符串了,而不是页面,写@Controller即可,不要写@ResponseBody。

 @GetMapping("/test")
    fun test(model: Model):String{
        model.addAttribute("name","ddddd")
        return "html/hello"
    }
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
</head>
<body>
name:<!--/*@thymesVar id="name" type="java.lang.String"*/-->
<span th:text="${name}"></span>
</body>
</html>

在这里片描述
这个是我的项目大致结构,可以参考下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值