加载中...

springboot整合thymeleaf

Thymeleaf

  • Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎
  • Spring官方支持的服务的渲染模板中,并不包含jsp。而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完美,几乎没有任何成本,你只用关注Thymeleaf的语法即可。

Thymeleaf的特点

  • 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
  • 开箱即用:它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  • 多方言支持:Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
  • 与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

SpringBoot整合

创建一个springboot项目,导入依赖

pom.xml

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

配置文件

application.properties

spring.application.name=springbootshiro
server.port=80

#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
#模板文件后缀名,默认html
spring.thymeleaf.suffix=.html
# 开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=UTF-8

# 静态文件请求匹配方式
#spring.mvc.static-path-pattern=/**

配置controller

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


@Controller
public class PageController {

    /**
     * 配置首页
     * @return
     */
    @RequestMapping({"/","index"})
    public String index(){
        return "index";
    }

    /**
     * 配置模板访问公共类
     * @return
     */
    @GetMapping("{module}/{page}.html")
    public String page(@PathVariable("module") String module, @PathVariable("page") String page){

        System.out.println(module);
        System.out.println(page);

        return module + "/" + page;
    }
}

thymelate使用参考文章:https://www.cnblogs.com/msi-chen/p/10974009.html

在html模板头部引入规范

<html lang="en" xmlns:th="https://www.thymeleaf.org/">

其他配置

资源打包问题

说明:之前使用springboot之前一些的版本,静态资源和配置文件不会自动打包,所以需要在pom文件配置下:

<build>
    <resources>
        <resource>
            <!--加载资源目录-->
            <directory>src/main/resources</directory>
            <includes>
                <!--加载配置文件-->
                <include>**/*.xml</include>
                <include>**/*.properties</include>
                <!--加载模板文件-->
                <include>**/*.html</include>
                <!--加载静态文件-->
                <include>/static/</include>
            </includes>

        </resource>
    </resources>
</build>

现在使用spring boot2.3.0版本没有发现这类问题,保存留作备份

配置错误页

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * 错误页面
 */
@Configuration
public class ErrorPageConfig implements ErrorPageRegistrar {
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        //404错误
        ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND,"/404.html");

        //500错误
        //ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/500.html");

        registry.addErrorPages(e404);
    }
}
posted @ 2020-05-24 11:42  royal6  阅读(343)  评论(0编辑  收藏  举报