app端文章详情展示

发布时间 2023-06-27 19:51:24作者: 佛系粥米

一、如何展示文章详情

  方案1:用户点击某一条文章,根据文章的id去查询文章内容表,返回渲染页面

  方案2:静态模板展示:根据文章内容通过模板技术生成静态的html文件,将该文件存入分布式文件系统minIO,并将生成好的html访问路径存入文章表中static_url。文章详情展示就通过该html去minIO中查找【效率更高,访问时不用查询数据库,直接访问到静态页面】

 

二、FreeMarker概述

  FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

(1)测试工程搭建

  创建一个freemarker-demo 的测试工程专门用于freemarker的功能测试与模板的测试。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>heima-leadnews-test</artifactId>
        <groupId>com.heima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>freemarker-demo</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- apache 对 java io 的封装工具库 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

</project>

配置文件

server:
  port: 8881 #服务端口
spring:
  application:
    name: freemarker-demo #指定服务名
  freemarker:
    cache: false  #关闭模板缓存,方便测试
    settings:
      template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
    suffix: .ftl               #指定Freemarker模板文件的后缀名

在freemarker的测试工程下创建模型类型用于测试

package com.heima.freemarker.controller;

import com.heima.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/basic")
    public String hello(Model model){

        //name
        model.addAttribute("name", "freemarker");
        //stu
        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        model.addAttribute("stu", student);
        return "01-basic";
    }
}

(2)freemarker 基础语法种类

      1、注释,即<#--  -->,介于其之间的内容会被freemarker忽略

      2、插值(Interpolation):即 **`${..}`** 部分,freemarker会用真实的值代替**`${..}`**

      3、FTL指令:和HTML标记类似,名字前加#予以区分,Freemarker会解析标签中的表达式或逻辑。

      4、文本,仅文本信息,这些不是freemarker的注释、插值、FTL指令的内容会被freemarker忽略解析,直接输出内容。

  集合List:<#list></#list>

  map:map['keyname'].property 或者 map.keyname.property

  if:<#if expression>

    <#else>

    </#if>

    freemarker中=与==相同

  运算符:+、-、*、/、%、==或=、!=、>或gt、>=或gte等

  空值处理:?? ,或者使用!要以指定一个默认值,当变量为空时显示默认值    例: ${name!''}表示如果name为空显示空字符串。

  内建函数:变量+?+函数名称   如集合大小:${集合名?size}、显示年月日: **`${today?date}`**、显示时分秒:**`${today?time}`**、显示日期+时间:**`${today?datetime}`**、自定义格式化: **`${today?string("yyyy年MM月")}`**

  内建函数C:model.addAttribute("point", 102920122);

        point是数字型,使用${point}会显示这个数字的值,每三位使用逗号分隔。

       如果不想显示为每三位分隔的数字,可以使用c函数将数字型转成字符串输出

       **`${point?c}`**

  将json字符串转成对象:<#assign text="{'bank':'工商银行','account':'10101920201920212'}" />

             <#assign data=text?eval />
             开户行:${data.bank} 账号:${data.account}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body>

<#-- list 数据的展示 -->
<b>展示list中的stu数据:</b>
<br>
<br>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#if stus??>
<#list stus as stu>
<#if stu.name=='小红'>
<tr style="color: red">
<td>${stu_index + 1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
<#else >
<tr>
<td>${stu_index + 1}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
</#if>

</#list>
</#if>

</table>
<hr>

<#-- Map 数据的展示 -->
<b>map数据的展示:</b>
<br/><br/>
<a href="###">方式一:通过map['keyname'].property</a><br/>
输出stu1的学生信息:<br/>
姓名:${stuMap['stu1'].name}<br/>
年龄:${stuMap['stu1'].age}<br/>
<br/>
<a href="###">方式二:通过map.keyname.property</a><br/>
输出stu2的学生信息:<br/>
姓名:${stuMap.stu2.name}<br/>
年龄:${stuMap.stu2.age}<br/>

<br/>
<a href="###">遍历map中两个学生信息:</a><br/>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#list stuMap?keys as key>
<tr>
<td>${key_index + 1}</td>
<td>${stuMap[key].name}</td>
<td>${stuMap[key].age}</td>
<td>${stuMap[key].money}</td>
</tr>
</#list>
</table>
<hr>

</body>
</html>

  

  (3)输出静态化文件

    模板index.ftl + 数据模型 通过freemarker生成index.html

    在配置文件中添加模板存放路径:template-loader-path: classpath:/templates

package com.heima.freemarker.test;

import com.heima.freemarker.FreemarkerDemoApplication;
import com.heima.freemarker.entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

@SpringBootTest(classes = FreemarkerDemoApplication.class)
@RunWith(SpringRunner.class)
public class FreemarkerTest {

    @Autowired
    private Configuration configuration;

    @Test
    public void test() throws IOException, TemplateException {
        Template template = configuration.getTemplate("02-basic.ftl");
        /**
         * 合成方法
         * 两个参数
         * 第一个模型数据
         * 第二个输出流
         */

        template.process(getData(), new FileWriter("d:/list.html"));
    }

    private Map getData(){
        Map<String, Object> map = new HashMap<>();


        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        //向model中存放List集合数据
        map.put("stus", stus);

        //------------------------------------

        //创建Map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        // 3.1 向model中存放Map数据
        map.put("stuMap", stuMap);
        map.put("point", 7823893232l);
        return map;
    }


}