freemarker入门简单教程
文章目录
集中学习了10个小时,算是入门了。写了一个简单的入门教程。
学习freemarker之前 ,如果掌握了任何一门编程语言应该是比较容易。
如果要熟悉掌握还是要下功夫的。本文入门主要是可以搭建环境,然后就可以做测试,不断加功能。
这样freemarker的语法就可以练习了。
日期: 2019-08-08
整理:xiaym
一、原理
这张图已经很直观的展示了freemarker的原理。freemarker最后生成的是静态页面。
二、安装
1. 新建一个maven工程
2. 在POP.XML中增加如下内容:
<?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">
<modelVersion>4.0.0</modelVersion>
<artifactId>testid</artifactId> <!-- 这个自己填一个 -->
<name>testname</name> <!-- 这个自己填一个 -->
<groupId>testgroupid</groupId> <!-- 这个自己填一个 -->
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId> <!-- 这个自己填一个 -->
<artifactId>freemarker</artifactId><!-- 这个自己填一个 -->
<version>2.3.23</version><!-- 这个是freemarker的版本-->
</dependency>
</dependencies>
<version>v1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. .m2/setting.xml
以下是仓库的配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>d:/maven</localRepository>
<pluginGroups>
<pluginGroup>net.hs.easyj</pluginGroup>
</pluginGroups>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://****/nexus/content/groups/public</url> <!--maven仓库的URL -->
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>hundsun-central-repos</id>
<url>http://****/nexus/content/groups/public</url> <!--maven仓库的URL -->
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>hundsun-central-repos</id>
<url>http://****/nexus/content/groups/public</url> <!--maven仓库的URL -->
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles>
<servers>
<server>
<id>nexus</id>
<username>****</username> <!--maven仓库的用户名 -->
<password>*****</password> <!--maven仓库的密码 -->
</server>
</servers>
</settings>
4. intellij 工程
三、示例
1. 掌握一个指令
在进行演示之前,先掌握一个指令。
<#assign seq = ["foo", "bar", "baz"]>
<#assign myname="xiaym" >
这个指令可以让我们在ftl模板中指定变量,而不需要从服务器上传过来。这样做是为了便于对指令进行练习。
另外前端开发人员可以直接设定这些变量做出效果页面来。
2. 掌握数据模型
再掌握一下freemarker的数据模型:
3. assign的例子
<html>
<head>
<meta?charset="utf-8">
<title>Freemarker入门教程?</title>
</head>
<style>
p
{
font-family:"Times New Roman";
font-size:15px;
background:#228FCF;
}
#rcorners1 {
border-radius: 15px;
background: #8AC007;
padding: 5px;
width: 500px;
height: 20px;
}
</style>
<body>
<#-- 这是直接演示列表的例子-->
<p id="rcorners1">这是直接演示列表的例子:</p>
<#assign list = ["foo", "bar", "baz"]>
<#list list as LsDemo>
<span>${LsDemo}</span>
</#list>
<p id="rcorners1">这是演示获取myname变量 </p>
${myname}
</body>
</html>
指令举例
现在可以学习其它的指令, 只是举了几个例子,其这的可以参考freemarker网站。freemarker中文网
if指令
<#assign ifdemo =3>
<#if ifdemo == 1>
<p id="rcorners1">ifdemo is 2</p>
<#elseif ifdemo == 2>
<p id="rcorners1">ifdemo is 2</p>
<#elseif ifdemo == 3>
<p id="rcorners1">ifdemo is 3</p>
</#if>
list指令
<#assign list = ["foo", "bar", "baz"]>
<#list list as LsDemo>
<span>${LsDemo}</span>
</#list>
item指令
<#assign users=["dog","cat","frog"] >
<#list users>
<ul>
<#items as user>
<li>${user}</li>
</#items>
</ul>
</#list>
注: item指令和list指令的区别。
item可以获取到list的子项。
四、完整的例子
test.ftl内容:
<html>
<head>
<meta?charset="utf-8">
<title>Freemarker入门教程</title>
</head>
<style>
p
{
font-family:"Times New Roman";
font-size:15px;
background:#228FCF;
}
#rcorners1 {
border-radius: 15px;
background: #8AC007;
padding: 5px;
width: 500px;
height: 20px;
}
</style>
<body>
<p id="rcorners1">这是直接演示列表的例子:</p>
<#assign listdemo = ["foo", "bar", "baz"]>
<#list listdemo as LsDemo>
<span>${LsDemo}</span>
</#list>
<#assign myname = "xiaym">
<p id="rcorners1">这是演示获取myname变量 </p>
${myname}
<p id="rcorners1">这是if指令演示:</p>
<#assign ifdemo =3>
<#if ifdemo == 1>
<p id="rcorners1">ifdemo is 2</p>
<#elseif ifdemo == 2>
<p id="rcorners1">ifdemo is 2</p>
<#elseif ifdemo == 3>
<p id="rcorners1">ifdemo is 3</p>
</#if>
<p id="rcorners1">这是list指令演示:</p>
<#assign listdemo = ["foo", "bar", "baz"]>
<#list listdemo as LsDemo>
<span>${LsDemo}</span>
</#list>
<p id="rcorners1">这是item指令演示:</p>
<#assign users=["dog","cat","frog"] >
<#list users>
<ul>
<#items as user>
<li>${user}</li>
</#items>
</ul>
</#list>
<p id="rcorners1">从java获取变量演示:</p>
${dogs}
${cats}
<p id="rcorners1">从java获取list演示:</p>
<#list list as LsDemo>
<span>${LsDemo}</span>
</#list>
</body>
</html>
test01.java的内容
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class test01 {
public static void main(String[] args) throws Exception {
// 1.设置配置类
Configuration configuration = new Configuration(Configuration.getVersion());
//2. 设置模板所在的目录
configuration.setDirectoryForTemplateLoading(
new File("D:\\SVN\\TA5.0\\Sources\\freemarker\\src\\main\\resources"));
//3.设置字符集
configuration.setDefaultEncoding("utf-8");
//4.加载模板
Template template = configuration.getTemplate("test.ftl");
//5.创建数据模型
HashMap animals = new HashMap();
List <String> list = new ArrayList<String>();
list.add("dog");
list.add("cat");
animals.put("list",list);
animals.put("dogs","DOGS");
animals.put("cats","CATS");
//6.创建Writer对象
FileWriter writer = new FileWriter(new File("D:\\freemarker\\test01.html"));
//7.输出数据模型到文件中
template.process(animals, writer);
//8.关闭Writer对象
writer.flush();
writer.close();
}
}