栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 整合 freemarker前端模板引擎实现数据展示

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 整合 freemarker前端模板引擎实现数据展示

一.freemaker 1.简介

FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker是免费的,基于Apache许可证2.0版本发布。其模板编写为FreeMarker Template Language(FTL),属于简单、专用的语言。需要准备数据在真实编程语言中来显示,比如数据库查询和业务运算, 之后模板显示已经准备好的数据。在模板中,主要用于如何展现数据, 而在模板之外注意于要展示什么数据

2.freemark构建

1、首先要构建一个freemark工程
步骤和之前构建springBoot项目大致相同,但是多了一步就是要勾选模板引擎中的apache freemark

2、导入pom依赖,更改资源文件
pom依赖:

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

application.yml配置文件:

在这里插入代码片server:
  port: 8080
spring:
  datasource:
#数据库名称与密码
    username: *****
    password: *****
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/crm?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true
  freemarker:
    #指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
    allow-request-override: false
    #req访问request
    request-context-attribute: req
    #后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
    #(如果默认不写此项的话,好像是得在Java中进行配置加载类,否则会出错)
    suffix: .ftl
 
    #设置响应的内容类型
    content-type: text/html;charset=utf-8
    #是否允许mvc使用freemarker
    enabled: true
    #是否开启template caching
    cache: false
    #设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
    template-loader-path: classpath:/templates/
    #设定Template的编码
    charset: UTF-8
#显示日志
logging:
  level:
    com.zxy.code.mapper: debug

注意
模板加载路径:要放在该路径下

#设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”] template-loader-path: classpath:/templates/

在Java中写配置freemaker的类

package com.chinaunicom.ibos.order.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.TemplateModelException;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;


@Configuration
public class FreemarkerConfig {

	@Value("${spring.profiles.active}")
	private String activeProfile;
	
	@Autowired
	private freemarker.template.Configuration config;
	
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() throws TemplateModelException{
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("classpath:/templates");
        Map variables = new HashMap<>(1);
        variables.put("param", "param");
        configurer.setFreemarkerVariables(variables);

        Properties settings = new Properties();
        settings.setProperty("default_encoding", "utf-8");
        settings.setProperty("number_format", "0.##");
        configurer.setFreemarkerSettings(settings);
        return configurer;
    }
    
    @PostConstruct
    public void setConfigure() throws Exception {
    	config.setSharedVariable("activeProfile", activeProfile);
    }
}

3、新建user界面
如果没有模板的话,到设置里面进行新建一个freemark模板

在生成的user.ftl模板中,通过${}的形式获取controller控制层中传的数据集




    
    


hello,家人们

msg:${msg}

<#if msg! != "">

msg:${msg}

<#list mapList! as users>

${users.userName}

${users.password}

4.创建controller控制层,使用ModelAndView返回数据


返回以上数据值,则表示 新建freemark模板成功

更多freemaker介绍:
https://blog.csdn.net/zxy15974062965/article/details/122818338

官网:
http://freemarker.foofun.cn/ref_builtins_boolean.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号