springboot之freemarker模板

1.新建项目是没有freemarker模板的,需要手动添加,或是新建html界面把后缀改为ftl

1.1:手动添加:

2.导入pom依赖

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

3.application.yml文件的默认配置

spring:
  thymeleaf:
    cache: false
  freemarker:
    # 设置模板后缀名
    suffix: .ftl
    # 设置文档类型
    content-type: text/html
    # 设置页面编码格式
    charset: UTF-8
    # 设置页面缓存
    cache: false
    # 设置ftl文件路径,默认是/templates,为演示效果添加role
    template-loader-path: classpath:/templates/role
  mvc:
    static-path-pattern: /static/**

4.相关代码:

前台代码:

list.ftl:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>角色列表</title>

    <#include 'common.ftl'>

</head>
<body>

角色列表

<h1>取值</h1>
<#--${name!未知}  !:后台返回的值为null时,会报错,用 ‘!’ 就不会报错了,而后面的值为:如果前面传来的值为空,则显示后面的默认值-->
welcome 【${name!'未知'}】 to page

<h1>非空判断</h1>
<#if name??>
    xxx
</#if>

<h1>条件表达式</h1>
<#if sex=='girl'>
    女
<#elseif sex=='boy'>
    男
<#else >
    girl
</#if>

<h1>循环</h1>
<table>
    <thead>
    <tr>
        <td>角色编号</td>
        <td>角色姓名</td>
        <td>角色简介</td>
    </tr>
    </thead>
    <tbody>
    <#list roles as role>
    <tr >
        <td>${role.rid}</td>
        <td>${role.rname}</td>
        <td>${role.rbak}</td>
    </tr>
    </#list>
    </tbody>
</table>


<h1>获取项目名(设置局部变量(assign)和全局变量(global))</h1>
<#--c:set-->
<#--jsp:${pageContext.request.contextPath}-->
<#--将项目名赋值给ctx1这个变量,这边的作用域在当前页面-->
<#assign ctxl>
    ${springMacroRequestContext.contextPath}
</#assign>
<#--将项目名赋值给ctx2这个变量,这边的作用域在整个项目-->
<#global ctx2>
  ${springMacroRequestContext.contextPath}
</#global>

${ctxl},${ctx2}

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

</body>
</html>

${springMacroRequestContext.contextPath}:SpringBoot中获取项目名 

foot.ftl:

底部内容

<a href="login.ftl">登录1</a>

<a href="${ctx2}/role/login">登录2</a>

common.ftl:

<#assign ctx>
  ${springMacroRequestContext.contextPath}
</#assign>

<base href="${ctx}/">

<script type="text/javascript" src="static/js/layuixxx.js">

</script>

login.ftl:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

欢迎来到登录界面

</body>
</html>

后台代码:

package com.spring.springboot02.entiry;

/**
 * @author cst
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2019-02-17 16:59
 */
public class Role {

    private Integer rid;
    private String rname;
    private String rbak;

    public Role() {
    }

    public Role(Integer rid, String rname, String rbak) {
        this.rid = rid;
        this.rname = rname;
        this.rbak = rbak;
    }

    public Integer getRid() {
        return rid;
    }

    public void setRid(Integer rid) {
        this.rid = rid;
    }

    public String getRname() {
        return rname;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public String getRbak() {
        return rbak;
    }

    public void setRbak(String rbak) {
        this.rbak = rbak;
    }
}
package com.spring.springboot02.controller;

import com.spring.springboot02.entiry.Role;
import com.spring.springboot02.entiry.User;
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 cst
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2019-02-17 16:00
 */

@Controller
public class IndexController {

    @RequestMapping("/role/list")
    public ModelAndView roleList(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");  //也可以写成 ‘role/list’ 但是application.yml里template-loader-path: classpath:/templates
        modelAndView.addObject("name",null);
        modelAndView.addObject("sex","gay");
        List list = new ArrayList();
        list.add(new Role(1,"教师","教授学业"));
        list.add(new Role(2,"学生","祖国的花朵"));
        modelAndView.addObject("roles",list);
        return modelAndView;
    }

 @RequestMapping("/role/login")
    public String login(){
        return "login";
    }


}

结果:

 

直接进入login.ftl会报错

 

5.freemarker模板也可以像jsp那样设置根路径:

<#assign ctx>
  ${springMacroRequestContext.contextPath}
</#assign>

<base href="${ctx}/">

<script type="text/javascript" src="static/js/layuixxx.js">

</script>

 

 


版权声明:本文为qq_40973475原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。