springboot的thymeleaf与freemarker模板

一.thymeleaf
注:其实就是html
1.先导入依赖

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

2.开发时建议关闭缓存,在配置文件中添加

spring:
  thymeleaf:
    cache: false
运行测试

controller相关代码

	    @RequestMapping("")
    private ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/user/list")
    private ModelAndView userlist() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> userlist=new ArrayList();
        userlist.add(new User(1,"AA","3"));
        userlist.add(new User(2,"BB","2"));
        userlist.add(new User(3,"CC","1"));
        modelAndView.addObject("title","学生列表");
        modelAndView.addObject("users",userlist);
        modelAndView.setViewName("/user/list");
        return modelAndView;
    }

html相关代码及常用th标签示例

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${title}">sss</h1><!--赋值,文本替换-->


<table style="border: 1px;width: 800px">
    <thead>
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user : ${users}"><!--遍历-->
        <td th:text="${user.uid}"></td>
        <td th:text="${user.uname}"></td>
        <td th:text="${user.desc}"></td>
    </tr>
    </tbody>
</table>

<select >
    <option th:each="user:${users}" th:value="${user.uid}" th:text="${user.uname}"></option>
</select>
</body>
</html>

一.freemarker
1.未添加模板先添加模板 settings–>file and code templates 找到file 然后点添加
Name填freemarker extension填ftl
内容直接把Html的复制进去
2.导入依赖

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

测试
controller相关代码

@RequestMapping("/role/list")
    private ModelAndView rolelist() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> userlist=new ArrayList();
        userlist.add(new User(1,"老师","3"));
        userlist.add(new User(2,"学生","2"));
        modelAndView.addObject("title","1");
        modelAndView.addObject("roles",userlist);
        modelAndView.setViewName("/role/list");
        return modelAndView;
    }

ftl相关代码及常用标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>${title!'2'}</h1>
<h1>非空判断</h1>
<#if title??>
    1
</#if>
<h1>条件表达式</h1>
    <#if title=='1'>
        男
    <#elseif title=='2'>
        女
    <#else>
        未知
    </#if>
<h1>遍历</h1>+
<table style="border: 1px;width: 800px">
    <thead>
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    </thead>
    <tbody>
    <#list roles as user>
    <tr>
        <td>${user.uid}</td>
        <td>${user.uname}</td>
        <td>${user.desc}</td>
    </tr>
    </#list>
    </tbody>
</table>

<h1>include</h1>
<#include 'foot.ftl'>

<h1>获取项目名</h1>
<#assign ctx1>
    ${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>
${ctx2}
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_41282789/article/details/87549918