J2EE开发各类资源下载清单, 史上最全IT资源,点击进入!
建立web项目,拷贝freemarker的jar文件到类路径,其他步骤略。
首先配置web.xml,添加freemarker解析的Servlet配置,内容如下
- <servlet>
- <servlet-name>freemarker</servlet-name>
- <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
- <init-param>
- <param-name>TemplatePath</param-name>
- <param-value>/</param-value>
- </init-param>
- <init-param>
- <param-name>NoCache</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>ContentType</param-name>
- <param-value>text/html;charset=UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>template_update_delay</param-name>
- <param-value>0</param-value>
- </init-param>
- <init-param>
- <param-name>default_encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>number_format</param-name>
- <param-value>0.##########</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>freemarker</servlet-name>
- <url-pattern>*.ftl</url-pattern>
- </servlet-mapping>
添加一个测试用的Servlet,代码如下:
- package test.servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class TestFreemarker extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String strValue = "this is String";
- Date myDate = new Date();
- Object[] objectArray = new Object[]{1,"str",1.2};
- Set<Object> setData = new HashSet<Object>();
- setData.add("dataValue1");
- setData.add("dataValue2");
- setData.add("dataValue3");
- List<String> strList = new ArrayList<String>();
- strList.add("字符串1");
- strList.add("字符串2");
- strList.add("字符串3");
- Map<String,Object> map = new HashMap<String, Object>();
- map.put("key1", "value1");
- map.put("key2", "value2");
- map.put("key3", "value3");
- req.setAttribute("strValue", strValue);
- req.setAttribute("objectArray", objectArray);
- req.setAttribute("strList", strList);
- req.setAttribute("map", map);
- req.setAttribute("myDate", myDate);
- req.setAttribute("setData", setData);
- req.getRequestDispatcher("/WEB-INF/view/freemarker/test.ftl").forward(req, resp);
- }
- }
配置此Servlet到web.xml中,增加如下内容到web.xml文件中:
- <servlet>
- <servlet-name>test</servlet-name>
- <servlet-class>test.servlet.TestFreemarker</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>/test</url-pattern>
- </servlet-mapping>
在/WEB-INF/view/freemarker目录下建立文件test.ftl,内容如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
- <head>
- <title>test!</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- </head>
- <body>
- <div>
- 直接显示字符串<br />
- String:${strValue}<br/>
- <hr/>
- </div>
- <div>
- 显示日期
- date:${myDate?date}<br/>
- <hr/>
- 显示时间
- time:${myDate?time}<br/>
- <hr/>
- 显示时间戳
- datetime:${myDate?datetime}<br/>
- <hr/>
- </div>
- <div>
- 访问对象数组
- ObjectArray:
- ${objectArray[0]},${objectArray[1]},${objectArray[2]}
- <br />
- <hr/>
- </div>
- <div>
- 遍历Set中的元素
- SetData:<br />
- <#list setData as value>
- ${value}
- </#list>
- <hr/>
- </div>
- <div>
- 遍历List中的元素
- strList:<br/>
- <#list strList as strValue >
- <#if strValue_index == 0>
- ${strValue} is first element
- <#elseif !strValue_has_next>
- ${strValue} is last element
- <#else>
- ${strValue}
- </#if>
- <br />
- </#list>
- <hr/>
- </div>
- <div>
- 获取map中的值
- <br/>
- map:
- key1=${map.key1},key2=${map.key2},key3=${map.key3}
- <hr/>
- </div>
- <#include "foot.html">
- </body>
- </html>
模板中包含的foot.html,在和test.ftl相同的目录下,文件内容如下
- <div style="margin-top: 50px;text-align: center;font-size: 12px">
- <hr />
- this is foot
- </div>
运行此项目访问/test路径,输出内容如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
- <head>
- <title>test!</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- </head>
- <body>
- <div>
- 直接显示字符串<br />
- String:this is String<br/>
- <hr/>
- </div>
- <div>
- 显示日期
- date:2011-3-18<br/>
- <hr/>
- 显示时间
- time:17:21:44<br/>
- <hr/>
- 显示时间戳
- datetime:2011-3-18 17:21:44<br/>
- <hr/>
- </div>
- <div>
- 访问对象数组
- ObjectArray:
- 1,str,1.2
- <br />
- <hr/>
- </div>
- <div>
- 遍历Set中的元素
- SetData:<br />
- dataValue2
- dataValue1
- dataValue3
- <hr/>
- </div>
- <div>
- 遍历List中的元素
- strList:<br/>
- 字符串1 is first element
- <br />
- 字符串2
- <br />
- 字符串3 is last element
- <br />
- <hr/>
- </div>
- <div>
- 获取map中的值
- <br/>
- map:
- key1=value1,key2=value2,key3=value3
- <hr/>
- </div>
- <div style="margin-top: 50px;text-align: center;font-size: 12px">
- <hr />
- this is foot
- </div></body>
- </html>
freemarker的list指令,不能遍历如Object[]类型的对象数组。但可以遍历List和Set