3.Spring Boot 整合thymeleaf

8.28 springboot_demo

https://www.jianshu.com/p/1e345a2c95de

https://www.jianshu.com/p/a842e5b5012e

添加thymeleaf依赖

<!--thymeleaf  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Spring Boot默认存放模板页面的路径在src/main/resources/templates或者src/main/view/templates
Thymeleaf默认的页面文件后缀是.html。

配置application.properties

#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end

引入相应的静态资源
image.png

显示Login页面

因为现在要显示的是thymeleaf的页面,不需要返回前台json格式,所以标签改回用@Controller,return的时候会经过视图解析器,找到对应的thymeleaf页面

登录页面AdminController
package com.moxi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @GetMapping("/login")
    public String login(Model model) {
        model.addAttribute("projectName", "MOXI");
        return "login";
    }

}

启动程序后,在网页输入网址 http://localhost:端口号/上下文路径/admin/login 就可以了。

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容