springboot模版

Thymeleaf模板

相關pom依賴

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

在這裏插入圖片描述

package com.zh.springboot01.entity;

import lombok.Data;

/**
 * @author lenovo
 * @create 2019-11-2019/11/8 11:29
 */
@Data
public class Dog {
    private String name;
    private Integer age;

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

package com.zh.springboot01.controller;

import com.zh.springboot01.entity.Dog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lenovo
 * @create 2019-11-2019/11/8 11:07
 */
@Controller
@RequestMapping("/thymeleaf")
public class TrymeleafController {

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", "aa");

        //循環
        List list = new ArrayList();
        list.add(new Dog("一一",9));
        list.add(new Dog("二二",5));
        list.add(new Dog("三三",7));
        mv.addObject("Dogs",list);
        //顯示html
        mv.addObject("msg","<span style='color:red;'>thymeleaf</span>");

        mv.setViewName("list");
        return mv;
    }

}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Thymeleaf</h1>

<h2>顯示文本</h2>
<span th:text="${name}"></span>

<h2>顯示html</h2>
<div th:utext="${msg}"></div>

<h2>循環</h2>
<table>
    <tr>
        <td>暱稱</td>
        <td>年齡</td>
    </tr>
    <tr th:each="d : ${Dogs}">
        <td th:text="${d.name}"></td>
        <td th:text="${d.age}"></td>
    </tr>
</table>

</body>
</html>

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

Freemarker模板

導入pom依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
package com.zh.springboot01.controller;

import com.zh.springboot01.entity.Dog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lenovo
 * @create 2019-11-2019/11/8 11:56
 */
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", "aa");
        mv.addObject("sex", "boy");
        //循環
        List list = new ArrayList();
        list.add(new Dog("一一",9));
        list.add(new Dog("二二",5));
        list.add(new Dog("三三",7));
        mv.addObject("Dogs",list);

        mv.setViewName("list");
        return mv;
    }

}
freemarker:
    # 設置模板後綴名
    suffix: .ftl
    # 設置文檔類型
    content-type: text/html
    # 設置頁面編碼格式
    charset: UTF-8
    # 設置頁面緩存
    cache: false
    # 設置ftl文件路徑,默認是/templates,爲演示效果添加role
    template-loader-path: classpath:/templates/role
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Freemarker</h1>

<h2>獲取值</h2>
${name!"默認值"}
${abc!"默認值"}

<h2>循環</h2>
<table border="1" width="50%">
    <tr>
        <td>暱稱</td>
        <td>年齡</td>
    </tr>
    <#list Dogs as d>
        <tr>
            <td>${d.name}</td>
            <td>${d.age}</td>
        </tr>
    </#list>
</table>

<h2>包含頁面</h2>
<#include 'commom/bao.ftl'>
<#include 'commom/global.ftl'>

<h2>exists用在邏輯判斷</h2>
<#if name?exists>
    ${name}
</#if>


<h2>局部變量(assign)/全局變量(global)</h2>
<#assign ctx1>
    ${springMacroRequestContext.contextPath}
</#assign>

<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>

${ctx1}和${ctx2}

<h2>如何在頁面定義變量</h2>
${ctx}


</body>
</html>

在這裏插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
被包含的頁面
</body>
</html>
<#global ctx>
    ${springMacroRequestContext.contextPath}
</#global>

局部變量和全局變量需要用到,所以改了一下:
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章