Freemarker实现网页静态化

1.什么是freemarker

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

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

2.freemarker的使用方法

这里使用的是maven工程 添加freemarker的jar包

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

实现原理:

使用步骤:

@Test
	public void testFreeMarker() throws Exception {
		//1.创建一个模板文件
		//2.创建一Configuration对象
		Configuration configuration = new Configuration(Configuration.getVersion());
		//3.设置模板文件保存目录
		configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-template-mars2/e3-item-web/src/main/webapp/WEB-INF/ftl"));
		//4.模板文件的编码格式 一般是UTF-8
		configuration.setDefaultEncoding("UTF-8");
		//5.加载一个模板文件 创建一个模板对象
		Template template = configuration.getTemplate("hello.ftl");
		//6.创建一个数据集 可以是pojo也可以是map 推荐使用map
		Map data = new HashMap<>();
		data.put("hello", "hello freeMarker");
		//7.创建一个writer 指定一个文件输出的路径及文件名
		Writer out = new FileWriter(new File("D:\\Test_freeMarker\\hello.txt"));
		//8.生成静态页面
		template.process(data, out);
		//9.关闭流
		out.close();

这里hello.ftl的内容是${hello}

2.1模板的语法

2.1.1访问map中的key

${key}

2.1.2访问pojo中的属性

${key.property}

2.1.3取集合中的数据

<#list studentList as student>

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

</#list>

2.1.4取循环中的下标

<#list studentList as student>

       ${student_index}

</#list>

2.1.5判断

<#if student_index % 2 == 0>

<#else>

</#if>

2.1.6日期类型格式化

2.1.7Null值的处理

2.1.8Include标签

<#include “模板名称”>

3.Freemarker整合spring

  1. 引入jar包
  2. 创建整合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>
    
  3. 测试

    @Controller
    public class HtmlGenController {
    	
    	@Autowired
    	private FreeMarkerConfigurer freeMarkerConfigurer;
    
    	@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";
    	}
    }
    

    4.Freemarker整合nginx

由于Tomcat比较适合处理jsp页面,不太适合处理静态html,项目中就需要Freemarker与nginx整合。

1.解压nginx压缩包

2.配置nginx.conf文件

root 后面修改为Freemarker生成静态页面的路径 (注意在Freemarker生成静态页面地址添加所需css js...)

测试:访问localhost/页面

最后将项目的href路径改为nginx访问路径  启动nginx.exe  最终实现页面静态化

猜你喜欢

转载自blog.csdn.net/GuiSu97/article/details/85161801