第一步: 定义模版文件:success.ftl <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> 姓名:${name}<br/> 姓名:${address}<br/> </body> </html> 第二步: 定义生成静态页的方法 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 javax.servlet.ServletContext; import freemarker.template.Configuration; import freemarker.template.Template; public class StaticFreemarker { public static void createHTML(ServletContext context,Map<String,Object>data,String templatePath,String targetHtmlPath) { Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading(context, "/WEB-INF/"); cfg.setEncoding(Locale.getDefault(), "utf-8"); Writer out = null; try { Template t = cfg.getTemplate(templatePath, "utf-8"); t.setEncoding("GB2312"); String htmlPath = context.getRealPath("/html")+"/"+targetHtmlPath; File htmlFile = new File(htmlPath); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8")); t.process(data, out); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(out != null){ try { out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } 第三步:调用并生成 public String createHTML()throws Exception{ Map<String,Object> data = new HashMap<String,Object>(); data.put("name", "Ivan"); data.put("address", "地球村"); StaticFreemarker.createHTML(request.getSession().getServletContext(), data, "success.ftl", "success.html"); return SUCCESS; //struts2跳转 }