springboot 整合 thymeleaf 页面

1、添加maven依赖

        <!-- 用于解析html -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、配置文件:application.properties

##thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.devtools.restart.enabled=true

spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true

3、目录结构

 

在resources下 新建 static & templates 两个目录,

static保存静态资源文件,templates保存html文件

4、新建Controller 访问路径

@Controller
public class IndexController {

    @RequestMapping({"/","/home"})
    public String index(HashMap map){
        return "home";
    }
}

访问localhost:8001/home 会跳转到home.html页面,

@ResponseBody

方法注解有这个,说明以json传值,不会返回页面,只返回json字符串。一般ajax会用到。

类注解 @Controller + 方法注解 @ResponseBody

相当于 类注解@RestController

5、新建页面 home.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">//代表 使用thymeleaf框架

<head>
    <meta charset="utf-8">
</head>

<body class="sticky-header">
    Hello,Springboot!

</body>
</html>

静态资源引入路径:/static/js/...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值