1.静的なページ
FreeMarkerのは、静的なページを達成するために使用することができます。
1.1 FreeMarkerのは何ですか
FreeMarkerのは、Java言語でありますテンプレートテンプレートに基づいているエンジンは、テキスト出力を生成します。FreeMarkerのがWebにあるWebコンテナ、とは何の関係もありません、それはサーブレットまたはHTTPを知りません。それだけでなく、使用することができますプレゼンテーション層実装技術が、また、生成するために使用することができますXML、JSP若しくはジャワなどなど。
現在、企業:Freemarkerの静的ページやページの表示回数の主な仕事
使用する1.2 Freemarkerの
FreeMarkerのジャーパッケージがプロジェクトに追加します。
Mavenのプロジェクトに依存している追加
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
基本的な考え方の概要:
使用手順は:
1.設定ファイルを設定します。
テンプレートを作成し、負荷2。
データを取得する3。
4.テンプレートにデータを追加します。
文書の5.出力ページ。
6.リリースリソース。
テンプレート:
$ {}テスト
@Test
public void genFile() throws Exception {
// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对应的版本号。
Configuration configuration = new Configuration(Configuration.getVersion());
// 第二步:设置模板文件所在的路径。
configuration.setDirectoryForTemplateLoading(new File("E:/workspace10/e3-item-web/src/main/webapp/WEB-INF/ftl"));
// 第三步:设置模板文件使用的字符集。一般就是utf-8.
configuration.setDefaultEncoding("utf-8");
// 第四步:加载一个模板,创建一个模板对象。
Template template = configuration.getTemplate("test.ftl");
// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
Map dataModel = new HashMap<>();
//向数据集中添加数据
dataModel.put("test", "this is my first freemarker test.");
// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
Writer out = new FileWriter(new File("E:/temp/out/test.html"));
// 第七步:调用模板对象的process方法输出文件。
template.process(dataModel, out);
// 第八步:关闭流。
out.close();
}
1.3。構文テンプレート
1.3.1。キーアクセスマップ
$ {キー}
1.3.2。プロパティのアクセスPOJO
学生対象。学生ID、氏名、年齢(あなたはStudentクラスのPOJOを作成する必要があります)
$ {} 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。Date型の書式
1.3.7処理NULL値
1.3.8。ラベルを含めます
<#include “模板名称”>
1.4。FreeMarkerの整合春
紹介のjarパッケージ:
Freemarkerのジャーパッケージ
1.4.1。統合春コンフィギュレーションファイルの作成
<?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>
あなたは、テストコントローラーを作成する必要があります
1.4.2。コントローラ
要求URL:/ GENHTML
パラメータ:なし
戻り値:OK(文字列、必要@ResponseBody)
ビジネスロジック:
1、コンテナから春FreeMarkerConfigurerオブジェクト。
図2に示すように、オブジェクトが設定FreeMarkerConfigurerオブジェクトから取得されます。
図3は、Configurationオブジェクトを使用すると、テンプレートオブジェクトを得ます。
4、データセットの作成
5を、出力ファイルは、ライターオブジェクトを作成しています。
6、プロセスメソッド呼び出しのテンプレートオブジェクトは、ファイルを生成します。
図7に示すように、ストリームが閉じられています。
コンフィギュレーション・ファイルをロードします:
@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("test.ftl");
// 4、创建数据集
Map dataModel = new HashMap<>();
dataModel.put("test", "1000");
// 5、创建输出文件的Writer对象。
Writer out = new FileWriter(new File("E:/temp/term197/out/spring-freemarker.html"));
// 6、调用模板对象的process方法,生成文件。
template.process(dataModel, out);
// 7、关闭流。
out.close();
return "OK";
}
}