博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成thymeleaf模板引擎
阅读量:4298 次
发布时间:2019-05-27

本文共 1709 字,大约阅读时间需要 5 分钟。

文章目录

SpringBoot集成thymeleaf模板引擎

一、前言

  • thymeleaf

Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。

Thymeleaf拥有用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择-尽管它可以做很多事情。

  • 开发环境

JDK 1.8+

  • 参考
  • thymeleaf:

  • spring initializr:

二、正文

  • spring initializr 中创建 SpringBoot 项目
  1. 默认创建了 demo 项目
  2. Dependencies:添加 String Web
  3. GENERATE

在这里插入图片描述

  • IntelliJ IDEA 打开项目

在这里插入图片描述

  • resources/static 文件夹:存放静态资源,浏览器可直接访问;如:static 下创建 static.html ,可输入 http://localhost:8085/static.html 访问。
  • resources/templates文件夹:存放动态资源,即模板页面。
  • pom.xml 添加 thymeleaf 的依赖
org.springframework.boot
spring-boot-starter-thymeleaf
  • application.properties 配置
# thymeleafspring.thymeleaf.mode=HTMLspring.thymeleaf.encoding=UTF-8spring.thymeleaf.servlet.content-type=text/htmlspring.thymeleaf.cache=false# springspring.application.name=thymeleaf-demoserver.port=8085
  • 创建文件夹和文件:com.example.thymeleafdemo.controller.DemoController.java
package com.example.thymeleafdemo.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class DemoController {
@GetMapping("/html/index") public ModelAndView goIndex(ModelAndView mav){
mav.addObject("name", "hello word"); mav.setViewName("test"); return mav; }}
  • 创建HTML: resources\templates\test.html

html标签: <html lang="en" xmlns:th="http://www.thymeleaf.org">

    
Test Page

Test Page

三、其它

1.其它模板引擎

(1)

(2)

2.跳转首页

浏览器:输入IP和端口后,默认跳转的页面,例如:http://localhost:8085

  • 默认跳转到 static/index.html
  • 如果 static/index.html 不存在,才会查找 templates/index.html

转载地址:http://wznws.baihongyu.com/

你可能感兴趣的文章
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day05
查看>>
学习笔记_vnpy实战培训day06
查看>>
Python super钻石继承
查看>>
回测引擎代码分析流程图
查看>>
Excel 如何制作时间轴
查看>>
股票网格交易策略
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
ubuntu终端一次多条命令方法和区别
查看>>
python之偏函数
查看>>
vnpy学习_06回测结果可视化改进
查看>>
读书笔记_量化交易如何建立自己的算法交易01
查看>>
设计模式03_工厂
查看>>
设计模式04_抽象工厂
查看>>