springboot之freemarker模板

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

1.1:手动添加:

springboot之freemarker模板_第1张图片

springboot之freemarker模板_第2张图片

2.导入pom依赖

  
       org.springframework.boot
       spring-boot-starter-freemarker
   

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:





  
  角色列表

    <#include 'common.ftl'>




角色列表

取值

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

非空判断

<#if name??> xxx

条件表达式

<#if sex=='girl'> 女 <#elseif sex=='boy'> 男 <#else > girl

循环

<#list roles as role>
角色编号 角色姓名 角色简介
${role.rid} ${role.rname} ${role.rbak}

获取项目名(设置局部变量(assign)和全局变量(global))

<#--c:set--> <#--jsp:${pageContext.request.contextPath}--> <#--将项目名赋值给ctx1这个变量,这边的作用域在当前页面--> <#assign ctxl> ${springMacroRequestContext.contextPath} <#--将项目名赋值给ctx2这个变量,这边的作用域在整个项目--> <#global ctx2> ${springMacroRequestContext.contextPath} ${ctxl},${ctx2}

include

<#include 'foot.ftl'>

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

foot.ftl:

底部内容

登录1

登录2

common.ftl:

<#assign ctx>
  ${springMacroRequestContext.contextPath}




login.ftl:




  
  Title



欢迎来到登录界面


后台代码:

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";
    }


}

结果:

springboot之freemarker模板_第3张图片

 

springboot之freemarker模板_第4张图片

直接进入login.ftl会报错

springboot之freemarker模板_第5张图片

 

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

<#assign ctx>
  ${springMacroRequestContext.contextPath}




springboot之freemarker模板_第6张图片

 

 

你可能感兴趣的:(SpringBoot)