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>

局部变量和全局变量需要用到,所以改了一下:
在这里插入图片描述
在这里插入图片描述

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