一些常用的小玩意之Freemarker

来源:互联网 发布:2017最新淘宝口令红包 编辑:程序博客网 时间:2024/05/01 17:00

1. 网页静态化

可以使用Freemarker实现网页静态化(真正静态化,不是伪静态)。

 

1.1. 什么是freemarker

FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarkerWeb容器无关,即在Web运行时,它并不知道ServletHTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XMLJSPJava等。

 

目前企业中:主要用Freemarker做静态页面或是页面展示

 

1.2. Freemarker的使用方法

freemarkerjar包添加到工程中。

Maven工程添加依赖

<dependency>

  <groupId>org.freemarker</groupId>

  <artifactId>freemarker</artifactId>

  <version>2.3.23</version>

</dependency>

 

原理:

 

使用步骤:

第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。

第二步:设置模板文件所在的路径。

第三步:设置模板文件使用的字符集。一般就是utf-8.

第四步:加载一个模板,创建一个模板对象。

第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map

第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。

第七步:调用模板对象的process方法输出文件。

第八步:关闭流。

 

模板:

${hello}

 

@Test

public void genFile()throws Exception {

// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。

Configuration configuration = new Configuration(Configuration.getVersion());

// 第二步:设置模板文件所在的路径。

configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-cctmall/term197/cct-item-web/src/main/webapp/WEB-INF/ftl"));

// 第三步:设置模板文件使用的字符集。一般就是utf-8.

configuration.setDefaultEncoding("utf-8");

// 第四步:加载一个模板,创建一个模板对象。

Template template = configuration.getTemplate("hello.ftl");

// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。

Map dataModel = new HashMap<>();

//向数据集中添加数据

dataModel.put("hello","this is my first freemarker test.");

// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。

Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));

// 第七步:调用模板对象的process方法输出文件。

template.process(dataModel,out);

// 第八步:关闭流。

out.close();

}

 

1.3. 模板的语法(主要用于展示复杂的界面)

1.3.1. 访问map中的key

${key}   不带引号

 

1.3.2. 访问pojo中的属性

Student对象。学号、姓名、年龄

 

${key.property}

 

 

1.3.3. 取集合中的数据

<#list studentList as student>

${student.id}/${studnet.name}

</#list>

 


 

1.3.4. 取循环中的下标

<#list studentList as student>

${student_index}

</#list>

 

1.3.5. 判断

<#if student_index % 2 == 0>

<#else>

</#if>

 

 

1.3.6. 日期类型格式化

 

1.3.7. Null值的处理

 

1.3.8. Include标签

<#include “模板名称”>

 

 

1.4. Freemarker整合springmvc

引入jar包:

Freemarkerjar

 

 

1.4.1. 创建整合spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

<bean id="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<property name="templateLoaderPath" value="/WEB-INF/ftl/" />

<property name="defaultEncoding" value="UTF-8" />

</bean>

 

 

</beans>

 

需要编写一Controller进行测试

 

1.4.2. Controller

请求的url/genhtml

参数:无

返回值:ok String, 需要使用@ResponseBody

业务逻辑:

1、spring容器中获得FreeMarkerConfigurer对象。

2、FreeMarkerConfigurer对象中获得Configuration对象。

3、使用Configuration对象获得Template对象。

4、创建数据集

5、创建输出文件的Writer对象。

6、调用模板对象的process方法,生成文件。

7、关闭流。

 

加载配置文件:

 

@Controller

public class HtmlGenController {

@Autowired

private FreeMarkerConfigurerfreeMarkerConfigurer;

 

@RequestMapping("/genhtml")

@ResponseBody

public String genHtml()throws Exception {

// 1、从spring容器中获得FreeMarkerConfigurer对象。

// 2、从FreeMarkerConfigurer对象中获得Configuration对象。

Configuration configuration = freeMarkerConfigurer.getConfiguration();

// 3、使用Configuration对象获得Template对象。

Template template = configuration.getTemplate("hello.ftl");

// 4、创建数据集

Map dataModel = new HashMap<>();

dataModel.put("hello","1000");

// 5、创建输出文件的Writer对象。

Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));

// 6、调用模板对象的process方法,生成文件。

template.process(dataModel,out);

// 7、关闭流。

out.close();

return "OK";

}

}

 

 

1.5. 商品详情页面静态化

1.5.1. 网页的静态化方案

输出文件的名称:商品id+.html

输出文件的路径:工程外部的任意目录。

网页访问:使用nginx访问网页。在此方案下tomcat只有一个作用就是生成静态页面。

工程部署:可以把cct-item-web部署到多个服务器上。

生成静态页面的时机:商品添加后,生成静态页面。可以使用Activemq,订阅topic(商品添加)

 





注:以上内容整理自互联网,如有侵权,请联系删除

注:以上内容整理自互联网,如有侵权,请联系删除
原创粉丝点击