struts2和freemarker生成静态页面

我们都要好好的.

---幸运皮


项目中用到了hibernate,所以,也有一些与这个无关的文件,相关的文件已经用红框框圈出来,具体步骤记录一下:

步骤一:

导入jar包,一个是freemarker的包,一个是在myeclipse中引入struts2包.freemarker的jar包可以在

http://www.java.sh/resource/library/freemarker2.3.18.html 下载.

步骤二:

配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package namespace="/" name="default" extends="struts-default"> <action name="index" class="com.guanghua.action.HtmlTest"> <result type="redirect">/${msg}</result> </action> </package> </struts>
步骤三:

完成工具类:CreateHtml.java:

package com.guanghua.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Locale; import java.util.Map; import org.apache.struts2.ServletActionContext; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class CreateHtml { /** * * @param ftl 模板文件 * @param htmlName 生成html文件 * @param map * @param relaPath * @throws IOException * @throws TemplateException */ public void init (String ftl,String htmlName,Map map,String relaPath) throws IOException,TemplateException{ Configuration cfg=new Configuration(); cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/"); cfg.setEncoding(Locale.getDefault(), "UTF-8"); Template template=cfg.getTemplate(ftl); template.setEncoding("UTF-8"); String path=ServletActionContext.getServletContext().getRealPath("/"); File fileName=new File(path+htmlName); Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8")); template.process(map, out); out.flush(); out.close(); } }
步骤四:

测试类:HtmlTest.java

package com.guanghua.action; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.guanghua.data.Mapping; import com.guanghua.util.CreateHtml; import com.opensymphony.xwork2.ActionSupport; import freemarker.template.TemplateException; public class HtmlTest extends ActionSupport{ private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String execute() { CreateHtml createHtml=new CreateHtml(); //Mapping 是数据库里面的一个表对应的java bean文件 List<Mapping> persons=new ArrayList<Mapping>(); for(int i=0;i<5;i++){ Mapping p=new Mapping(); p.setId(i); p.setChs("你好"+i); p.setCht("cht"+i); p.setEng("eng" + i); p.setJan("jan" + i); persons.add(p); } Map<String,List<Mapping>> map=new HashMap<String, List<Mapping>>(); map.put("personlist", persons); String htmlName="personlist.html"; String ftl="person.ftl"; String relaPath="index.jsp"; try { createHtml.init(ftl, htmlName, map, relaPath); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } this.msg=htmlName; return SUCCESS; } }
步骤五:

模板person.ftl:

<html> <head> <title></title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <table style="text-align:center;FONT-SIZE: 11pt; WIDTH: 600px; FONT-FAMILY: 宋体; BORDER-COLLAPSE: collapse" borderColor=#3399ff cellSpacing=0 cellPadding=0 align=center border=1> <tr> <td><b>编号</b></td> <td><b>简体中文</b></td> <td><b>繁体中文</b></td> <td><b>英文</b></td> <td><b>日文</b></td> </tr> <#list personlist as person> <tr> <td>${person.id}</td> <td>${person.chs}</td> <td>${person.cht}</td> <td>${person.eng}</td> <td>${person.jan}</td> </tr> </#list> </body> </html>


最后访问index.action

ps: 模板里面本来没有头文件,总是出乱码,后来加上了就好了.

参考:http://rwhaoo.blog.163.com/blog/static/171908088201096105147871/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值