1. 程式人生 > >- SpringBoot整合Thymeleaf模板

- SpringBoot整合Thymeleaf模板

Spring Boot提供了多種模板引擎的預設配置支援,但嵌入式容器JSP有限制,2010年後Velocity停止更新,所以這JSP與Velocity兩個不建議使用,然實際在企業中,還是有很多在使用

那麼這裡主要是springboot整合Thymeleaf模板。
SpringBoot使用上述模板,預設從src/main/resources/templates下載入。

Thymeleaf是現代化伺服器端的Java模板引擎,不同與其它幾種模板的是Thymeleaf的語法更加接近HTML,並且具有很高的擴充套件性,其他詳細可移步官網

依賴

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

先建立一個實體類
entity

package com.zyc.entity;

public class Person {
    private String name;
    private String gender;
    private String email;
//get set 方法
}

建立一個controller 用來對映HTTP請求與頁面的跳轉

controller

package com.zyc.controller;

import com.zyc.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import
javax.servlet.http.HttpServletRequest; /** * @Description: Thymeleaf * @author zhaopeng * @email [email protected] */ @Controller @RequestMapping public class ThyController { @GetMapping("/index") public ModelAndView index() { ModelAndView view = new ModelAndView(); view.setViewName("index"); // 設定屬性 view.addObject("title", "Thymeleaf頁面"); view.addObject("desc", "Welcome to Thymeleaf"); Person person = new Person(); person.setGender("男娃子"); person.setEmail("[email protected]"); person.setName("趙鵬"); view.addObject("person", person); return view; } @GetMapping("/index1") public String index1(HttpServletRequest request) { // 設定屬性 request.setAttribute("title", "Thymeleaf頁面"); request.setAttribute("desc", "Welcome to Thymeleaf"); Person person = new Person(); person.setGender("難道到這裡就能變成個女娃子?"); person.setEmail("[email protected]"); person.setName("趙鵬"); request.setAttribute("person", person); return "index"; } }

兩個方法的寫法不同,但是結果一致。
在第一個方法中設定的跳轉到index,預設對映到 src/main/resources/templates/{viewName}.html
在第一個方法中設定的跳轉到index,預設對映到 src/main/resources/templates/xxxx.html

這樣就基本ok了,可以先直接將這個index.html開啟看一下,這是靜態效果
在這裡插入圖片描述

SpringBoot 預設情況下為我們做了如下的預設配置工作,當然我們也可以在自己需要的情況下去修改這些配置,實現自己的需求
在這裡插入圖片描述

好的到這裡本篇文章就先到此了,創作不易,如果那裡有不合適的地方還請大家多多指教,寫這篇博的目的主要就是為了方便自己以後的一個回憶和朋友學習時的一個參考,希望為大家可以帶來幫助 ~ ~&

虛心的去學習,自信的去工作~

相關推薦

3.SpringBoot整合Thymeleaf模板

一、前言 SrpingBoot支援如JSP、Thymeleaf、FreeMarker、Mustache、Velocity等各種模板引擎,同時還為開發者提供了自定義模板擴充套件的支援。 使用嵌入式Servlet容器時,請避免使用JSP,因為使用JSP打包後會存在一些限制。 在SpringBoot使用上述模

SpringBoot整合Thymeleaf模板

1、新增起步依賴: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactI

- SpringBoot整合Thymeleaf模板

Spring Boot提供了多種模板引擎的預設配置支援,但嵌入式容器JSP有限制,2010年後Velocity停止更新,所以這JSP與Velocity兩個不建議使用,然實際在企業中,還是有很多在使用 那麼這裡主要是springboot整合Thymelea

SpringBoot整合thymeleaf(自定義)模板中文亂碼的解決辦法

樓主今天在學習SpringBoot整合thymelaf的時候報了中文亂碼的錯誤,經過網上的搜尋,現在得到解決的辦法,分享給大家: package com.imooc.config; import org.springframework.beans.BeansExcepti

Springboot系列:SpringbootThymeleaf模板引擎整合基礎教程(附原始碼)

前言 由於在開發My Blog專案時使用了大量的技術整合,針對於部分框架的使用和整合的流程沒有做詳細的介紹和記錄,導致有些朋友用起來有些吃力,因此打算在接下來的時間裡做一些基礎整合的介紹,當然,可能也不會特別的基礎,但是原始碼會開放給大家,方便大家學習,此次的原始碼地址為s

記一次SpringBoot整合thymeleaf layout模板的經歷

在thymeleaf中可以使用th:include等標籤在模板引擎載入的時候動態的將要包含的資料引進頁面,但是當頁面較多且每一個頁面都具有相同部分時,這樣做比較麻煩,所以百度找到可以使用layout模板 第一步:引入依賴 <dependency&g

springbootthymeleaf模板的paginate分頁

display source for charset lan 引擎 封裝 protected hiberna 本文根據一個簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼(新手自學,不足之處歡迎糾正); 先看java部分 pom.xm

springboot mail+Thymeleaf模板

from ann () ria sage code pool color final compile ‘org.springframework.boot:spring-boot-starter-thymeleaf‘ compile ‘io.ratpack:rat

Spring Boot 2.0 整合Thymeleaf 模板引擎

reporting 配置信息 name www. title 建模 type 引擎 suffix 本節將和大家一起實戰Spring Boot 2.0 和thymeleaf 模板引擎 1. 創建項目 2. 使用Spring Initlizr 快速創建Spring Boot

springbootthymeleaf模板問題springbootthymeleaf模板問題

fix pid source pen html5標簽 需要 嚴格 thymeleaf cef springboot項目引入thymeleaf模板時,默認thymeleaf模板對html5標簽是嚴格檢查的。要實現非嚴格檢查 1. 在項目中加NekoHTML庫 在Maven中添

SpringBootThymeleaf模板

視圖解析 web 應用 text true fix content return uuid ddr 閱讀目錄 一、前言二、集成 Thymeleaf 模板引擎三、使用 Thymeleaf 模板回到頂部一、前言Thymeleaf 的出現是為了取代 JSP,雖然 JSP 存在了很

Spring Boot 整合 Thymeleaf 模板引擎

原文地址:Spring Boot 整合 Thymeleaf 模板引擎 Thymeleaf 是適用於 Web 和獨立環境的現代伺服器端 Java 模板引擎。 Thymeleaf 的主要目標是為開發帶來優雅的模板 - Thymeleaf 模板對於瀏覽器來說這些非HTML標準屬性在渲染

Spring Boot (Web 篇):整合Thymeleaf模板

  目錄 說在前面 Thymeleaf特點 在pom.xml加入相關依賴 配置application.properties Controller層 Thymeleaf頁面 專案啟動 第一種方式,SpringBoot直接啟動(內建整合tomcat)

Springboot整合Thymeleaf、layui實現簡單的增刪改查

Springboot整合Thymeleaf、layui實現簡單的增刪改查 主頁面列表分頁顯示 搜尋功能 新增使用者 編輯使用者 Mapper對映 Controller層 list.html初始化載入表格資料 搜尋過載表

spring boot2.0一看就會的超詳細快速入門(六)-整合Thymeleaf模板引擎

1.理解Thymeleaf 1.1 Thymeleaf是一個適用於Web和獨立環境的伺服器端java模板引擎,能夠處理HTML、XML、JavaScript、CSS甚至純檔案,常見類似的工具有JSP、Freemarker等 1.2 Thymeleaf遵循的是一個自然模板的概念,主要目標是提供一種優雅且高度

SpringBoot 整合thymeleaf報錯:Caused by: java.lang.ClassNotFoundException: org.thymeleaf.resourceresolver

最近在學springboot,聽說很火很方便,但學習時問題好多啊,今天一早又報錯!!!還是啟動報錯!!! java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoc

SpringBoot 整合 Thymeleaf 實現增刪改查,實現前後端分離做法

通過一個簡單的與Springboot整合Demo認識Thymeleaf模板 文章目錄 通過一個簡單的與Springboot整合Demo認識Thymeleaf模板 什麼是Thymeleaf Thymeleaf 的基礎使用 前後端分離

SpringBoot整合Freemarker模板技術

    FreeMarker 是一款 模板引擎: 即一種基於模板和要改變的資料,並用來生成輸出文字(HTML網頁,電子郵件,配置檔案,原始碼等)的通用工具。它不是面向終端使用者的,而是一個Java類庫,是一款程式設計師可以嵌入他們所開發產品的元件。  模板編寫為FreeMarke

springboot 整合thymeleaf

直接上程式碼... 1.maven引用 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.

第四篇:整合Thymeleaf模板

第四篇:整合Thymeleaf模板   本文連結:http://blog.battcn.com/2018/04/28/springboot/v2-web-thymeleaf/ 在前面幾章中已經介紹瞭如何建立一個SpringBoot專案,同時簡單的描述