Web项目中使用Freemarker
FreeMarker是一款模板引擎:一种基于模板的、用来生成输出文本的通用工具。
Web项目中,有时用其作为视图层技术来生成响应的html,其实下载的freemarker压缩包中已经有很多例子介绍如何在项目中使用。如图,有jsp的、struts中使用的等等,很详细。
下面还是就简单介绍一下如何使用,方便以后回忆:
准备:添加freemarker.jard到项目fmk
一、直接使用Freemarker模板替代jsp、html页面
1.web.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> <head> <title>Freemarker练手</title> </head> <body> <#assign msg="你好,这是Freemarker发出的消息"> <h1>Freemarker说:</h1> ${msg} </body> </html>
3.注意:
项目编码需要和web.xml中配置的freemarker编码一致,不然会出现乱码
4.访问:
下面就可以通过URL【http://localhost:8080/fmk/】来访问了
二、使用Servlet作为控制器
这里加入servlet作为控制器,给Freemarker模板传入数据。
1.新建servlet:SimpleMsgServlet.java
package com.kyj.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class SimpleMsgServlet extends HttpServlet { private static Configuration cfg; @Override public void init() throws ServletException { if (cfg == null) { cfg = new Configuration(); cfg.setServletContextForTemplateLoading(getServletContext(),"WEB-INF/template"); } } @Override protected void service(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { //传给模板用来存储数据的model Map<String, Object> model = new HashMap<String, Object>(); //传给模板的list数据 List<String> authors = Arrays.asList(new String[]{"James","Killer","King"}); //传给模板的map数据 Map<String, Object> viewLogs = new HashMap<String, Object>(); viewLogs.put("ROOT", "2012-12-31 23:43:11"); viewLogs.put("LOG_VIEWER", "2012-12-31 23:43:11"); viewLogs.put("DATA_MANAGER", "2012-12-31 23:43:11"); //添加到model中 model.put("viewLogs", viewLogs); model.put("authors", authors); //加载模板输出 Template t = cfg.getTemplate("SimpleMsg.ftl"); res.setCharacterEncoding("UTF-8"); t.setEncoding("UTF-8"); PrintWriter out = res.getWriter(); try { t.process(model, out); } catch (TemplateException e) { e.printStackTrace(); } } }
<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
<#ftl encoding="UTF-8"> <html> <head> <title>Freemarker简单Servlet消息</title> </head> <body> <h1>下面是Servlet传过来的信息</h1> <#-- list数据 --> <h4>作者列表</h4> <ul> <#list authors as author> <li>${author}</li> </#list> </ul> <#-- map数据 --> <h4>管理员最后登录时间列表</h4> <table cellpadding="1" border='1' cellspacing="1"> <tr> <th width='120' align='center'>用户名</th> <th width='200' align='center'>最后登录时间</th> </tr> <#list viewLogs?keys as record> <tr> <td>${record}</td> <td align='center'>${viewLogs[record]}</td> </tr> </#list> </table> </body> </html>
3.web.xml配置servlet
<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提供的例子。
相关推荐
81314797 2020-11-18
89314493 2020-11-03
81941231 2020-09-17
thisisid 2020-09-09
如狼 2020-08-15
82384399 2020-06-16
86384798 2020-05-12
80183053 2020-05-02
86384798 2020-04-26
86384798 2020-04-11
rionchen 2020-04-09
86384798 2020-04-07
86384798 2020-04-04
80183053 2020-03-07
87201943 2020-03-06
83961233 2020-02-26
87201943 2020-02-21