1. 程式人生 > >Springboot+Thymeleaf配置與使用

Springboot+Thymeleaf配置與使用

前言
Springboot預設是不支援JSP的,預設使用thymeleaf模板引擎。所以這裡介紹一下springboot使用Thymeleaf的例項以及遇到的問題。

配置與使用
1.在application.properties檔案中增加Thymeleaf模板的配置。
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

說明一下,這些配置不是必須的,如果配置了會覆蓋預設的。 
在開發時建議將spring.thymeleaf.cache設定為false,否則會有快取,導致頁面沒法及時看到更新後的效果。 
比如你修改了一個檔案,已經update到tomcat,但重新整理頁面還是之前的頁面,就是因為快取引起的。

2.在pom.xml中新增thymeleaf的依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1
2
3
4
3.編寫一個測試的Controller
@RequestMapping(value = "/greeting")
public ModelAndView test(ModelAndView mv) {
    mv.setViewName("/greeting");
    mv.addObject("title","歡迎使用Thymeleaf!");
    return mv;
}

可以看到,Controller與普通的SpringMVC的Controller無異。

4.編寫greeting.html
spring-boot專案靜態檔案目錄:/src/java/resources/static 
spring-boot專案模板檔案目錄:/src/java/resources/templates 
所以greeting.html檔案在/src/java/resources/templates下。

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link th:href="@{/css/1.css}" rel="stylesheet"/>
</head>
<body>
<p th:text="'Hello, ' + ${title}" /><br/>

<script th:src="@{/js/jquery/1.11.0/jquery.js}"></script>
<script>
    $(function(){
       alert("page load finish.");
    });
</script>
</body>
</html>

相關推薦

Springboot+Thymeleaf配置使用

前言 Springboot預設是不支援JSP的,預設使用thymeleaf模板引擎。所以這裡介紹一下springboot使用Thymeleaf的例項以及遇到的問題。 配置與使用 1.在application.properties檔案中增加Thymeleaf模板的配置。 #t

SpringBoot-基礎配置資料庫訪問技術

本文的核心內容:SpringBoot的基礎配置、SpringBoot資料庫訪問技術。 SpringBoot的配置在resources目錄下,application.properties。 SpringBoot的配置可以分為兩種,一種為基礎配置如伺服器資訊、日誌等;另一

SpringBoot日記——SpringMvc自動配置擴展篇

讀取 發送 registry 設置 取數據 gmv inf com 自動配置 為了讓SpringBoot保持對SpringMVC的全面支持和擴展,而且還要維持SpringBoot不寫xml配置的優勢,我們需要添加一些簡單的配置類即可實現; 通常我們使用的最多的註解是:

springbootTHYMELEAF 配置

# THYMELEAF (ThymeleafAutoConfiguration) spring.thymeleaf.cache=true # Whether to enable template caching. spring.thymeleaf.check-template=true # Wh

SpringBoot + mybatis預設配置手動配置步驟對比一(預設配置

前提環境已經搭建完成 Eclipse + SpringBoot + mybatis預設配置: 1.建立工程 File->new->other->Spring Boot -> Spring Starter Project ->選擇jdk等資訊

SpringBoot在eclipse中的配置入門

1.1 eclipse下安裝Springboot 下載與本機安裝的Eclipse對應的STS(Spring Tool Suite) 檢視本機eclipse的版本號:在eclipse下Help--About Eclipse 我安裝的eclipse為 4.5.2

SpringBoot分散式管理,新增日誌記錄,註解配置EhCache使用

    首先看下專案結構: 首先看下pom.xml配置的jar包:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

springboot(二):springboot配置檔案application.propertiesappliction.yml

前言:在springboot中.properties與.yml是兩種不同格式的配置檔案,         .properties是xxx.xxx.xxx=xxx的格式,    &nb

springBoot+Hibernate(Jpa)多資料來源配置使用

在學習的過程中,大多時候專案與資料庫都在本機上,使用hibernate或者mybatits加上簡單的配置就能夠打通程式到資料庫路徑,讓程式能夠訪問到資料庫。然而在一些情況下,我們不僅需要訪問本機的資料庫,還需要訪問到另外一個數據庫中的資源。本文書寫的目的便在於解

使用外部tomcat執行springBoot+thymeleaf專案並指定配置檔案

1.啟動類繼承SpringBootServletInitializer並重寫config方法 public class BootdoApplication extends SpringBootServletInitializer{ @Override protected

SpringBoot配置測試使用

這裡總結一下spring剛開始使用時遇到的一些問題,我想對於初學者應該有些幫忙。一,配置巢狀的問題我在專案中使用到了redis,但是沒有使用spring-boot-data-redis的包,而是自己封裝的Jedis客戶端。我需要在配置中配置一下redis的連線池等相關資訊:applic

SpringBoot結合Thymeleaf模板Bootstrap快速搭建介面

前言 本系列文章將簡單的學習SpringCloud微服務相關知識,其實也是因為時間的原因,一直拖到現在,遂打算趁著假期,決定記錄下來。 從天氣預報微服務系統的單體架構——>分散式架構的演變過程中,一步一步,由淺及深的學習SpringCloud微

SpringBoot之——LogBack日誌配置多環境實戰

一、Slf4j+Logback的快速實踐spring-boot預設支援logback,所以無需引用任何以來只需要,配置application.properties即可,如果要功能豐富些,則配置下logback.xml。1、在application.properties裡配置的

springboot 多資料來源配置使用

多資料來源配置 application.properties 配置兩個資料庫 #資料庫配置# spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver spring.datasourc

springboot thymeleaf和shiro 整合 第二篇 配置許可權和認證

package com.ruiguang.config;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import

SpringBoot原理分析配置

1.1 起步依賴原理分析 1.1.1 分析spring-boot-starter-parent 按住Ctrl點選pom.xml中的spring-boot-starter-parent,跳轉到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重點配置): <

Linux - vim安裝 配置使用

格式 only ctx net height border term mona 方便 一 Vim 簡單介紹 曾經一直用vi,近期開始使用 vim,以下將兩者做一下比較。 vi和vim都是多模式編輯器,不同的是vim 是vi的升級版本號,它不僅兼容vi的全部指令,並且

Flask入門之SQLAlchemy配置數據庫連接

數據 rdo nsh imp log kref mod pre file 1. 安裝SQLAlchemy pip install flask-sqlalchemy 2. 導入和配置 from flask_sqlalchemy import SQLAlchemy bas

centos EMQTTD 集群安裝配置測試驗證

centos emqttd 集群安裝配置與測試驗證安裝包下載地址:http://emqtt.com/downloads在這裏我們下載 emqttd-centos6.8-v2.1.2.zip配置兩臺主機hosts192.168.0.10 s1.emqtt.io 192.168.0.20 s2.emqtt.i

【ADO.NET】1、簡單配置使用

字符串 文件中 .exe 增加 獲取字符串 pass 數據庫連接 rect manage 1、一些基礎的知識點 ExecuteReader(); //返回查詢到的數據,一次一行,用於 selectExecuteNonQuery(); //返回影響的行數,用於 delete,