java模板引擎,springboot11 模板引擎

 2023-11-30 阅读 36 评论 0

摘要:11、模板引擎 前端交給我們的頁面,是html頁面。如果是我們以前開發,我們需要把他們轉成jsp頁面,jsp好處就是當我們查出一些數據轉發到JSP頁面以后,我們可以用jsp輕松實現數據的顯示,及交互等。 jsp支持非常強大的功能,包括能寫Java

11、模板引擎
前端交給我們的頁面,是html頁面。如果是我們以前開發,我們需要把他們轉成jsp頁面,jsp好處就是當我們查出一些數據轉發到JSP頁面以后,我們可以用jsp輕松實現數據的顯示,及交互等。

jsp支持非常強大的功能,包括能寫Java代碼,但是呢,我們現在的這種情況,SpringBoot這個項目首先是以jar的方式,不是war,像第二,我們用的還是嵌入式的Tomcat,所以呢,他現在默認是不支持jsp的。

那不支持jsp,如果我們直接用純靜態頁面的方式,那給我們開發會帶來非常大的麻煩,那怎么辦呢?

java模板引擎?SpringBoot推薦你可以來使用模板引擎:

模板引擎,我們其實大家聽到很多,其實jsp就是一個模板引擎,還有用的比較多的freemarker,包括SpringBoot給我們推薦的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他們的思想都是一樣的,什么樣一個思想呢我們來看一下這張圖:
在這里插入圖片描述
模板引擎的作用就是我們來寫一個頁面模板,比如有些值呢,是動態的,我們寫一些表達式。而這些值,從哪來呢,就是我們在后臺封裝一些數據。然后把這個模板和這個數據交給我們模板引擎,模板引擎按照我們這個數據幫你把這表達式解析、填充到我們指定的位置,然后把這個數據最終生成一個我們想要的內容給我們寫出去,這就是我們這個模板引擎,不管是jsp還是其他模板引擎,都是這個思想。只不過呢,就是說不同模板引擎之間,他們可能這個語法有點不一樣。其他的我就不介紹了,我主要來介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個高級語言的模板引擎,他的這個語法更簡單。而且呢,功能更強大。

我們呢,就來看一下這個模板引擎,那既然要看這個模板引擎。首先,我們來看SpringBoot里邊怎么用。

11.1、引入Thymeleaf
怎么引入呢,對于springboot來說,什么事情不都是一個start的事情嘛,我們去在項目中引入一下。給大家三個網址:

Springboot框架。Thymeleaf 官網:https://www.thymeleaf.org/

Thymeleaf 在Github 的主頁:https://github.com/thymeleaf/thymeleaf

Spring官方文檔:找到我們對應的版本

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

springboot前端模板?找到對應的pom依賴:可以適當點進源碼看下本來的包!

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

Maven會自動下載jar包,我們可以去看下下載的東西;
在這里插入圖片描述
11.2、Thymeleaf分析
前面呢,我們已經引入了Thymeleaf,那這個要怎么使用呢?

我們首先得按照SpringBoot的自動配置原理看一下我們這個Thymeleaf的自動配置規則,在按照那個規則,我們進行使用。

我們去找一下Thymeleaf的自動配置類:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING;public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;private boolean checkTemplateLocation = true;private String prefix = "classpath:/templates/";private String suffix = ".html";private String mode = "HTML";private Charset encoding;
}

java引擎?我們可以在其中看到默認的前綴和后綴!

我們只需要把我們的html頁面放在類路徑下的templates下,thymeleaf就可以幫我們自動渲染了。

使用thymeleaf什么都不需要配置,只需要將他放在指定的文件夾下即可!
11.3、測試
1、編寫一個TestController

@Controller
public class TestController {@RequestMapping("/test")public String testcontroller(){return "test";}
}

@Controller 用于標記在一個類上,使用它標記的類就是一個SpringMVC Controller 對象。分發處理器將會掃描使用了該注解的類的方法,并檢測該方法是否使用了@RequestMapping 注解

springboot返回html,@RequestMapping 來映射URL 到控制器類,或者是到Controller 控制器的處理方法上。當@RequestMapping 標記在Controller 類上的時候,里面使用@RequestMapping 標記的方法的請求地址都是相對于類上的@RequestMapping 而言的;當Controller 類上沒有標記@RequestMapping 注解時,方法上的@RequestMapping 都是絕對路徑。這種絕對路徑和相對路徑所組合成的最終路徑都是相對于根路徑“/ ”而言的。

2、編寫一個測試頁面 test.html 放在 templates 目錄下
在這里插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>我是test</title>
</head>
<body>
<h1>Test!</h1>
</body>
</html>

3、啟動項目請求測試
在這里插入圖片描述
小結:

只要需要使用thymeleaf,只需要導入對應的依賴就可以了,我們需要將html頁面放在我們的templates目錄下即可

maven java。11.4、Thymeleaf 語法學習
要學習語法,還是參考官網文檔最為準確,我們找到對應的版本看一下;

Thymeleaf 官網:https://www.thymeleaf.org/ , 簡單看一下官網!我們去下載Thymeleaf的官方文檔!

要使用thymeleaf,需要在html文件中導入命名空間的約束

xmlns:th="http://www.thymeleaf.org"

測試

springboot前后端分離。1、修改測試請求,增加數據傳輸;

@Controller
public class TestController {@RequestMapping("/test")public String testcontroller(Model model){model.addAttribute("msg","hello springboot");return "test";}
}

補充:addAttribute往前臺傳數據,可以傳對象,可以傳List,通過el表達式 ${}可以獲取到

語法
addAttribute(name,value,ns);
name
必需。
規定要添加的屬性的名稱。
value
可選。
規定屬性的值。
ns
可選。
規定屬性的命名空間。

2、編寫下前端頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>我是test</title>
</head>
<body>
<h1>Test!</h1><!--注意:  所有的html元素都可以被thymeleaf替換接管;   th: 元素名-->
<!--th:text 就是將div中的內容設置為它指定的值,和之前學習的Vue一樣--><div th:text="${msg}"></div>
</body>
</html>

Spring boot?3、測試
在這里插入圖片描述
來認真研習一下Thymeleaf的使用語法!

1、我們可以使用任意的 th:attr 來替換Html中原生屬性的值!

在這里插入圖片描述
2、我們能寫哪些表達式呢?

Simple expressions:(表達式語法)
Variable Expressions: ${...}:獲取變量值;OGNL;1)、獲取對象的屬性、調用方法2)、使用內置的基本對象:#18#ctx : the context object.#vars: the context variables.#locale : the context locale.#request : (only in Web Contexts) the HttpServletRequest object.#response : (only in Web Contexts) the HttpServletResponse object.#session : (only in Web Contexts) the HttpSession object.#servletContext : (only in Web Contexts) the ServletContext object.3)、內置的一些工具對象:#execInfo : information about the template being processed.#uris : methods for escaping parts of URLs/URIs#conversions : methods for executing the configured conversion service (if any).#dates : methods for java.util.Date objects: formatting, component extraction, etc.#calendars : analogous to #dates , but for java.util.Calendar objects.#numbers : methods for formatting numeric objects.#strings : methods for String objects: contains, startsWith, prepending/appending, etc.#objects : methods for objects in general.#bools : methods for boolean evaluation.#arrays : methods for arrays.#lists : methods for lists.#sets : methods for sets.#maps : methods for maps.#aggregates : methods for creating aggregates on arrays or collections.
==================================================================================Selection Variable Expressions: *{...}:選擇表達式:和${}在功能上是一樣;Message Expressions: #{...}:獲取國際化內容Link URL Expressions: @{...}:定義URL;Fragment Expressions: ~{...}:片段引用表達式Literals(字面量)Text literals: 'one text' , 'Another one!' ,…Number literals: 0 , 34 , 3.0 , 12.3 ,…Boolean literals: true , falseNull literal: nullLiteral tokens: one , sometext , main ,…Text operations:(文本操作)String concatenation: +Literal substitutions: |The name is ${name}|Arithmetic operations:(數學運算)Binary operators: + , - , * , / , %Minus sign (unary operator): -Boolean operations:(布爾運算)Binary operators: and , orBoolean negation (unary operator): ! , notComparisons and equality:(比較運算)Comparators: > , < , >= , <= ( gt , lt , ge , le )Equality operators: == , != ( eq , ne )Conditional operators:條件運算(三元運算符)If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)Default: (value) ?: (defaultvalue)Special tokens:No-Operation: _

測試:

springboot流程引擎,1、編寫一個Controller

@Controller
public class Test2Controller {@RequestMapping("/test2")public String test2(Map<String,Object> map){map.put("msg1","<h1>Hello</h1>");map.put("users", Arrays.asList("李泌","張小敬"));return "test2";}
}

2、測試頁面取出數據

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>我是test2</title>
</head>
<body>
<h1>我是test2頁面</h1><!--不轉義-->
<div th:text="${msg1}"></div>
<!--轉義-->
<div th:utext="${msg1}"></div><!--遍歷數據-->
<!--th:each每次遍歷都會生成當前這個標簽:官網#9-->
<h4 th:each="user:${users}" th:text="${user}"></h4><h4><!--行內寫法:官網#12--><span th:each="user:${users}">[[${user}]]</span>
</h4></body>
</html>

3、測試
在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/186158.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息