当前位置: 代码迷 >> Web前端 >> freemarker简洁总结
  详细解决方案

freemarker简洁总结

热度:554   发布时间:2012-11-10 10:48:51.0
freemarker简要总结

准备工作:

》安装freemarker eclipse插件:

???????? If you use Eclipse 3.x:

  1. (open eclipse)Help -> Software updates -> Find and install....
  2. Choose "Search for new features to install".
  3. Click Add Update Site..., and type "FreeMarker" for Name and "http://www.freemarker.org/eclipse/update" for URL.
  4. Check the box of the "FreeMarker" feature.
  5. "Next"-s until it is installed...

》下载freemarker java类库(freemarker.jar

?

建立项目:

new projectct

import freemarker.jar

》建立模板(我是先建立html文件,然后更改后缀名为ftl)详细建立过程后面说明

》建立数据模型(new class class 里面建立数据模型)详细建立过程后面说明

new servelet ,在servelet 显示模板+数据模型的输出结果,详细建立过程后面说明

》配置tomcat以及jdk

run project as java application

start tomcat

》用浏览器访问servelet ??e.g. ?http://localhost8080/projectname/servelet

?

简单的模板+数据模型:

拿一个简单的项目来做模板+数据模型的演示

数据模型(以下只是视图表现,只供方便浏览):

Root

?+? Animal

?????????????????? +Name dog

?????????????????? ?Price? 5

?????????????????? +Name pig

?????????????????? ?Price? 10

数据模型(java代码实现根据视图模型):

Map root = new HashMap();

??????? List animal = new ArrayList();//animal List类型

??????? Map item = new HashMap();

??????? item.put("name", "dog");

??????? item.put("price", 5);

??????? animal.add(item);?????????? //添加dog animal

???????

??????? Map item1 = new HashMap();

??????? item1.put("name","pig");

??????? item1.put("price", 10);

??????? animal.add(item1);???????? //添加pig animal

??????? root.put("animal", animal);

模板(模板中用到上面的数据模型中的数据):

<html>

<head>

? <title>Welcome!</title>

</head>

<body>

??? <#list animal as temp>???? <#-- 列举animal 里面的所有项,按照上面的模型,

??????????????????????????????? ?? 则是包含namedogpig price510

???????????????????????????????????????? ―>

?????? <#if temp.name= "dog"> <#-- 条件指令-->

?????????? <p>dog's information :

?????? <#else>

?????????? <p>pig's information :

?????? </#if>

?????? <p>? animal's name = ${temp.name}? animal's prcie = ${temp.price}

??? </#list>

</body>

</html>?

?》该例子中用到两种指令:if 条件指令 list循环指令(不仅仅起循环浏览作用,还有其他排序等作用,目前只是简单了解,以后加入详细其他说明)

?? <#list animal?sort_by("name")as temp>//按照name进行排序

<#if condition><#elseif condition><#else> 条件判断


<#list hash_or_seq as var>
遍历hash表或者collectionfreemaker称作sequence)的成员

${…}:称为interpolationsFreeMarker会在输出时用实际值进行替代

?

?

建立的project中需要被修改的地方(针对自己建的project):

Project包含四部分:InitFreeMarker、数据模型classserveletftl模板(由网页编辑人员完成)

?? InitFreeMarker部分(黄色部分可能需要修改,其他地方一律不需要改):

import java.io.File;

import java.io.StringWriter;

import java.util.HashMap;

import java.util.Map;

?

import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

public class InitFreeMarker {

???????? protected Configuration cfg ;

???????? protected StringWriter outPut = new StringWriter() ; //output=data+model

???????? protected Template temp ; //模板

???????? InitFreeMarker()throws Exception{

?????????????????? cfg = new Configuration();

?????????????????? //设置模板存放路径

?????????????????? cfg.setDirectoryForTemplateLoading(

???????????????????????????????????? new File("D:/Program Files/eclipse/freemarker/WebRoot/WEB-INF/templates")); //可能需要修改,根据模板存放位置

?????????????????? cfg.setObjectWrapper(new DefaultObjectWrapper());

??? }

???????? private void designDataModel()throws Exception{}

}

?

?? 数据模型class(黄色部分可能需要修改,其他地方一律不需要修改)

继承InitFreeMarker,代码如下:

import freemarker.template.*;

import java.util.*;

import java.io.*;

?

public class Test extends InitFreeMarker {

???????? Test()throws Exception{??????????

?????????????????? temp = cfg.getTemplate("test.ftl");//找到模板,指定模板名字

?????????????????? this.designDataModel();?? //调用数据模型设计及模板数据处理方法

???? }

????????

???????? private void designDataModel()throws Exception{//重载设计数据模型及合并数据模板

?????????????????? ?/* 以下部分是建立数据模型,由java程序员完成*/

??????? Map root = new HashMap();

??????? root.put("user", "Big Tom");

??????? Map latest = new HashMap();

??????? root.put("latestProduct", latest);

??????? latest.put("url", "products/greenmouse.html");

??????? latest.put("name", "green mouse");????

???????

??????? temp.process(root, outPut);//合并数据模板

??????????????????

???????? }

}??

?

?? Servelet(黄色部分需要修改,其他地方一律不需要修改)

import java.io.IOException;

import java.io.PrintWriter;

?

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

?

?

public class WelcomeServelet extends HttpServlet {

???????? public void doGet(HttpServletRequest request, HttpServletResponse response)

??????????????????????????? throws ServletException, IOException {

?

?????????????????? response.setContentType("text/html");

?????????????????? PrintWriter out = response.getWriter();

??????????????????

?????????????????? try {

??????????????????????????? Test temp = new Test();//生成模板+数据实例

??????????????????????????? out.println(temp.outPut.toString());? //把数据+模板结果生成到浏览器上

?????????????????? } catch (Exception e) {

??????????????????????????? e.printStackTrace();

?????????????????? }

???????? }

}