页面静态化 freemarker的安装和应用注意点

本文详细介绍如何在Eclipse环境中安装并配置JBossTools插件,利用Freemarker模板引擎进行Java开发。通过实例演示了如何创建Freemarker模板文件,并结合Java实体类和集合数据渲染成HTML页面。


安装:

网上搜索jbosstools ,进入jbosstools下载JBoss Tools , 注意JBOSS 版本和 Eclipse的版本要匹配.





安装JBoss Tools,

选择JBoss Application Development 下的FreeMarker IDE和Hibernate Tools,如图,

可选择性安装FreeMarker和Hibernate插件



安装完按提示重启Eclipse即可.


应用:

使用maven构建一jar工程,添加依赖freemarker

创建两个freemaker文件

hello.ftl:


student.ftl:

<html>
	<head>学生信息</head>

	<body>
		学生信息列表:<br>
		<table border="1">
			<tr>
				<th>序号</th>
				<th>学号</th>
				<th>姓名</th>
				<th>年龄</th>
			</tr>
		<#list studentList as stu>
			<#if stu_index % 2==0>
				<tr bgcolor="red">
				<#else>
				<tr bgcolor="green">
			</#if>
			<td>${stu_index}</td>
			<td>${stu.xuehao}</td>
			<td>${stu.name}</td>
			<td>${stu.age}</td>
			</tr>
		</#list>
		</table>
		
	<!-- 可以在key后面使用?date 或者?time或者?datetime ?string(pattern)来告诉模板时间的具体格式-->
	当前日期:${date?string('yyyy/MM/dd HH:mm:ss')}
	<!-- val! 代表如果val为空 -->
	空值:${val!"我是默认值nullnull"}<br/>
	空值2:<!-- 判断val2是否为空 ??代表不为空 -->
		<#if val2??>
			val2有内容,内容是${val2}
			<#else>
			val2的内容为空
		</#if>
		
	<br>----------华丽的分割线-----------------<br>
		<#include "hello.ftl">
	</body>

</html>

创建一Java实体类Student

package freemarktest;

public class Student {
	private int xuehao;
	private String name;
	private int age;
	
	public Student() {
		super();
	}
	
	public Student(int xuehao, String name, int age) {
		super();
		this.xuehao = xuehao;
		this.name = name;
		this.age = age;
	}

        //getter and setter方法
}


测试类:

package com.qx.testfreemarker;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarktest.Student;

public class FreeMarkerTest {

	public static void main(String[] args) throws Exception {
		//加载模板
		//1 加载模板的配置对象
		Configuration configuration=new Configuration(Configuration.getVersion());
		//2 指定模板的目录
		configuration.setDirectoryForTemplateLoading(new File("G:\\javaEEWeb_Eclipse\\workspace\\freemarktest\\src\\test\\resources"));
		//3 设置字符集
		configuration.setDefaultEncoding("UTF-8");
		//4 加载具体模板
		Template template = configuration.getTemplate("student.ftl");
		
		Map<String, Object> values=new HashMap<>();
		values.put("name", "王五");
		
		
		List<Student> students=new ArrayList<>();
		students.add(new Student(1,"张珊珊",20));
		students.add(new Student(2, "掌勺七", 12));
		students.add(new Student(3, "卢西奥费", 21));
		students.add(new Student(4, "洛霞", 21));
		values.put("studentList", students);
		values.put("date", new Date());
		values.put("val", null);
		values.put("val2", "你看到的是一个假的空值");
		//指定静态化后输出目录
		Writer writer=new FileWriter(new File("d:\\freemarker\\22.html"));
		template.process(values, writer);
		writer.close();
	}
}

在 d 盘下创建一文件夹 freemarker


运行测试方法,则在D盘freemarker下生成了22.html文件:




注意:

1.

spring中使用freemarker,要同时添加下面3个依赖.否则编译时就报错

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

		<bean id="freemarkerconfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
			<property name="templateLoaderPaths" value="G:\javaEEWeb_Eclipse\workspace\freemarktest\src\test\resources"></property>
			<property name="defaultEncoding" value="UTF-8"></property>
		</bean>
</beans>


在手机端,没显示全,特放上图片





2. freemaker的for循环及隔行变色:

注意: 索引是  stu_index而不是stu.index,  这和 jsp不同


3 freemarker的判空  和  include语法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wudinaniya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值