freemarker

FreeMarker是一款模板引擎:一种基于模板的、用来生成输出文本的通用工具。

Web项目中,有时用其作为视图层技术来生成响应的html,其实下载的freemarker压缩包中已经有很多例子介绍如何在项目中使用。如图,有jsp的、struts中使用的等等,很详细。

下面还是就简单介绍一下如何使用,方便以后回忆:

准备:添加freemarker.jard到项目fmk

一、直接使用Freemarker模板替代jsp、html页面

1.web.xml配置

Xml代码

<!--Freemarker模板servlect-->

<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</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>locale</param-name>

<param-value>en_US</param-value>

</init-param>

<init-param>

<param-name>number_format</param-name>

<param-value>0.##</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>freemarker</servlet-name>

<url-pattern>*.ftl</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.ftl</welcome-file>

</welcome-file-list>

2.freemarker的欢迎页index.ftl

Html代码

<html>

<head>

<title>Freemarker练手</title>

</head>

<body>

<#assignmsg="你好,这是Freemarker发出的消息">

<h1>Freemarker说:</h1>

${msg}

</body>

</html>

3.注意:

项目编码需要和web.xml中配置的freemarker编码一致,不然会出现乱码

4.访问:

下面就可以通过URL【http://localhost:8080/fmk/】来访问了

二、使用Servlet作为控制器

这里加入servlet作为控制器,给Freemarker模板传入数据。

1.新建servlet:SimpleMsgServlet.java

Java代码

packagecom.kyj.servlet;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.Arrays;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importfreemarker.template.Configuration;

importfreemarker.template.Template;

importfreemarker.template.TemplateException;

publicclassSimpleMsgServletextendsHttpServlet{

privatestaticConfigurationcfg;

@Override

publicvoidinit()throwsServletException{

if(cfg==null){

cfg=newConfiguration();

cfg.setServletContextForTemplateLoading(getServletContext(),"WEB-INF/template");

}

}

@Override

protectedvoidservice(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{

//传给模板用来存储数据的model

Map<String,Object>model=newHashMap<String,Object>();

//传给模板的list数据

List<String>authors=Arrays.asList(newString[]{"James","Killer","King"});

//传给模板的map数据

Map<String,Object>viewLogs=newHashMap<String,Object>();

viewLogs.put("ROOT","2012-12-3123:43:11");

viewLogs.put("LOG_VIEWER","2012-12-3123:43:11");

viewLogs.put("DATA_MANAGER","2012-12-3123:43:11");

//添加到model中

model.put("viewLogs",viewLogs);

model.put("authors",authors);

//加载模板输出

Templatet=cfg.getTemplate("SimpleMsg.ftl");

res.setCharacterEncoding("UTF-8");

t.setEncoding("UTF-8");

PrintWriterout=res.getWriter();

try{

t.process(model,out);

}catch(TemplateExceptione){

e.printStackTrace();

}

}

}

Xml代码

<servlet>

<servlet-name>simpleMsg</servlet-name>

<servlet-class>com.kyj.servlet.SimpleMsgServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>simpleMsg</servlet-name>

<url-pattern>/simpleMsg</url-pattern>

</servlet-mapping>

2.WEB-INF/template下新建模板文件:SimpleMsg.ftl

Java代码

<#ftlencoding="UTF-8">

<html>

<head>

<title>Freemarker简单Servlet消息</title>

</head>

<body>

<h1>下面是Servlet传过来的信息</h1>

<#--list数据-->

<h4>作者列表</h4>

<ul>

<#listauthorsasauthor>

<li>${author}</li>

</#list>

</ul>

<#--map数据-->

<h4>管理员最后登录时间列表</h4>

<tablecellpadding="1"border='1'cellspacing="1">

<tr>

<thwidth='120'align='center'>用户名</th>

<thwidth='200'align='center'>最后登录时间</th>

</tr>

<#listviewLogs?keysasrecord>

<tr>

<td>${record}</td>

<tdalign='center'>${viewLogs[record]}</td>

</tr>

</#list>

</table>

</body>

</html>

3.web.xml配置servlet

Java代码

<servlet>

<servlet-name>simpleMsg</servlet-name>

<servlet-class>com.kyj.servlet.SimpleMsgServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>simpleMsg</servlet-name>

<url-pattern>/simpleMsg</url-pattern>

</servlet-mapping>

4.访问servlet

http://localhost:8080/fmk/simpleMsg

三、与Struts框架整合

见freemarker提供的例子。

相关推荐