springboot整合templates模板引擎


 

 

templates簡介:

 

  • 市面上的模板引擎有:JSP、Velocity、Freemarker、Thymeleaf;

  • 之前開發用jsp當摸吧引擎,但是springboot的特殊機制,使得springboot不支持jsp頁面,但這樣會有很多開發上的不方便,所以他支持了Thymelead模板引擎。

 

我這里就不詳細介紹templates的原理,百度上面一大堆,這主要講講templates的用法。

 

首先是maven的依賴:

    <!-- 模板引擎 thymeleaf的依賴-->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 

然后在application.properties的配置項里面配置:

#配置項,
#這里主要配置模板引擎的數據

#thymeleaf配置
#模板的模式,支持如:HTML、XML、TEXT、JAVASCRIPT等
spring.thymeleaf.mode=HTML5
#編碼,可不用配置
spring.thymeleaf.encoding=UTF-8
#內容類別,可不用配置
spring.thymeleaf.servlet.content-type=text/html
#開發配置為false,避免修改模板還要重啟服務器
spring.thymeleaf.cache=false
#配置模板路徑,默認就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/

配置項好之后,那么就剩下實現了。

而模板文件時要放到tempates里面的,所以要先在類路徑下面創建這個文件夾,然后在說其他的。如下:

 

 

接下來寫寫怎么呈現集成成功。

這個是home.html,放在webapp下面,因為都可以通過項目來跳級訪問資源,所以放在templates下面的那些模板文件可以和這互相訪問。

 

以下是home.html的實現:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
</head>
<body>
<p>在webapp下面的資源可以訪問到templates下面的資源,原因是webapp在加載的時候會放到classpath下面去</p>
<p>所以可以進行交互,互相訪問</p>
    <a href="templates/index">index</a>
    <a href="templates/back">more</a>
</body>
</html>

controller的代碼:

package start.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 控制類
 * @author 徐金仁
 *
 */
@Controller
@RequestMapping("templates")
public class Controller_ {
    
    @RequestMapping("index")
    public String showIndex(ModelMap map){
        map.addAttribute("username", "蘑菇頭");
        map.addAttribute("password", "123456789");
        map.put("tes", "你好");
        return "index";
    }
    
    
    
    /**
     * 關於模板引擎的注解,如果要返回一個視圖層的話就不能使用@ResponseBody注解,
     * 使用@ResponseBody注解的話,會將返回的視圖當做字符串來處理返回,就起不到模板引擎的效果
     * 還有,上面控制類的注解:@Controller和@RestController這兩個注解要區分開來
     * @RestController = @Controller + @ResponseBody
     * @param map
     * @return
     */
    /*@ResponseBody*/
    @RequestMapping("back")
    public String showback(ModelMap map){
        
        map.put("key1", "/templates/back/more.html,在controller返回"
                + "視圖層的時候,可以省略后面的html后綴,添加上頁沒有什么關系");
        
        map.put("arr", new String[]{"張三","李四","王五","趙六"});
        
        map.put("name", "<font color = 'red'>趙婷</font>");
        
        //ModelMap中的put和addAttribute同樣是添加數據,那么有什么區別?
        
        //map.put(null, "null_put");
        
        //map.addAttribute(null,"null_addAttribute");
        
        //結果證明:put方法是能添加空值的,而addAttribute是不能添加空值的
        
        //當put和addAllAttribute方法添加了相同的key值,會出什么問題。
        
        map.put("key_common", "key_common_put");
        
        map.addAttribute("key_common","key_common_addAttribute");
        
        //結果顯示,會存在相同key會覆蓋的問題,后面添加的數據會將前面一個key值的數據覆蓋掉
        
        return "/back/more.html";
    }
    
}

index.html如下:

<html  xmlns:th="http://www.thymeleaf.org"  lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
</head>
<body>
<p>在使用模板引擎的過程中,在后台添加了數據的話,就會顯示后台存的數據:</p>
    <span th:text = "${username}"></span>
    <span th:text = "${password}"></span>
    <span th:text="${tes}"></span>
</body>
</html>

 

 

more.html如下:

<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
</head>
<body>
    <p th:text="${key1}"></p>
    <br/>
    <p>模板引擎簡單的循環:</p>
            <p  th:each = "item : ${arr}" th:text="${item}"></p>
        
    <p th:text="${name}"></p>    
    <p>當數據中含有html要解析的時候,用到utext</p>
    <p th:utext="${name}"></p>
    
    <p th:text="${null}">1</p>
    <p th:text="${null}">2</p>
    <p th:text="${key_common}"></p>
</body>
</html>

 

結果如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

思路來源:https://blog.csdn.net/qq_29726359/article/details/88043652

     https://blog.csdn.net/ignorewho/article/details/80692094

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM