springboot之freemarker模板

本文介绍了如何在SpringBoot项目中集成并配置Freemarker模板引擎。首先,由于新建项目默认不包含Freemarker,需要手动添加或者通过修改文件后缀实现。接着,添加相关依赖到pom.xml,并配置application.yml文件,包括模板后缀、内容类型、字符集等。然后,展示了Freemarker模板的使用,包括不同页面(如list.ftl、foot.ftl、common.ftl、login.ftl)的编写。最后,提到在使用过程中可能遇到的问题,如直接访问login.ftl会导致错误,并提示Freemarker可以像JSP那样设置根路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值