Struts2和Freemarker整合应用静态页面
利用Struts2生成静态页面其实很灵活,很强大,尤其是利用Struts2对Freemarker较好的支持,充分利用Freemarker的模板功能来生成静态 页面。
基本思路为:利用Struts2对自定义result type的支持,自定义能够生成静态页面的result type,结合模板引擎Freemarker可以实现大批量静态页面的生成。
参看org.apache.struts2.views.freemarker.FreemarkerResult的代码实现,自定义了自己的生成静态页 面的result type。此种方案不单纯用于生成静态页面,其实也可以用于生成诸如wml、xhtml等内容,具体可以参考Struts2缺省提供的各种result type的实现。
1 package com.mobilesoft.esales.webapp.action; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.OutputStreamWriter; 7 import java.io.Writer; 8 import java.util.Locale; 9 import javax.servlet.ServletContext; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import org.apache.struts2.ServletActionContext; 13 import org.apache.struts2.dispatcher.StrutsResultSupport; 14 import org.apache.struts2.views.freemarker.FreemarkerManager; 15 import org.apache.struts2.views.util.ResourceUtil; 16 import com.opensymphony.xwork2.ActionContext; 17 import com.opensymphony.xwork2.ActionInvocation; 18 import com.opensymphony.xwork2.LocaleProvider; 19 import com.opensymphony.xwork2.inject.Inject; 20 import com.opensymphony.xwork2.util.ValueStack; 21 import freemarker.template.Configuration; 22 import freemarker.template.ObjectWrapper; 23 import freemarker.template.Template; 24 import freemarker.template.TemplateException; 25 import freemarker.template.TemplateModel; 26 import freemarker.template.TemplateModelException; 27 28 public class FreemarkerResult extends StrutsResultSupport { 29 private static final long serialVersionUID = -3778230771704661631L; 30 protected ActionInvocation invocation; 31 protected Configuration configuration; 32 protected ObjectWrapper wrapper; 33 protected FreemarkerManager freemarkerManager; 34 private Writer writer; 35 protected String location; 36 private String pContentType = “text/html”; 37 protected String fileName; // 要生成的静态页面名称 38 protected String filePath; // 要生成的静态页面的路径 39 protected String staticTemplate; // 用于生成静态页面Freemarker模板的路径 40 public FreemarkerResult() { 41 super(); 42 } 43 public FreemarkerResult(String location) { 44 super(location); 45 } 46 @Inject 47 public void setFreemarkerManager(FreemarkerManager mgr) { 48 this.freemarkerManager = mgr; 49 } 50 public void setContentType(String aContentType) { 51 pContentType = aContentType; 52 } 53 public String getContentType() { 54 return pContentType; 55 } 56 public void doExecute(String location, ActionInvocation invocation) 57 throws IOException, TemplateException { 58 this.location = location; 59 this.invocation = invocation; 60 this.configuration = getConfiguration(); 61 this.wrapper = getObjectWrapper(); 62 this.fileName = (String) conditionalParse(fileName, invocation); 63 this.staticTemplate = (String) conditionalParse(staticTemplate, invocation); 64 this.filePath = ((String) conditionalParse(filePath, invocation)) == null ? “” 65 : ((String) conditionalParse(filePath, invocation)); 66 if (!location.startsWith(”/”)) { 67 ActionContext ctx = invocation.getInvocationContext(); 68 HttpServletRequest req = (HttpServletRequest) ctx 69 .get(ServletActionContext.HTTP_REQUEST); 70 String base = ResourceUtil.getResourceBase(req); 71 location = base + “/” + location; 72 } 73 //生成html页面的模板类 74 Template template = configuration.getTemplate(location, deduceLocale()); 75 // 生成静态页面的的模板类 76 Template staticTemplate = configuration.getTemplate(this.staticTemplate, 77 deduceLocale()); 78 TemplateModel model = createModel(); 79 String path = ServletActionContext.getServletContext().getRealPath( 80 filePath) 81 + File.separator; 82 Writer out = new BufferedWriter(new OutputStreamWriter( 83 new FileOutputStream(path + fileName))); 84 if (preTemplateProcess(template, model)) { 85 try { 86 staticTemplate.process(model, out); 87 template.process(model, getWriter()); 88 } finally { 89 postTemplateProcess(template, model); 90 postTemplateProcess(staticTemplate, model); 91 } 92 } 93 } 94 protected Configuration getConfiguration() throws TemplateException { 95 return freemarkerManager.getConfiguration(ServletActionContext 96 .getServletContext()); 97 } 98 protected ObjectWrapper getObjectWrapper() { 99 return configuration.getObjectWrapper(); 100 } 101 public void setWriter(Writer writer) { 102 this.writer = writer; 103 } 104 protected Writer getWriter() throws IOException { 105 if (writer != null) { 106 return writer; 107 } 108 return ServletActionContext.getResponse().getWriter(); 109 } 110 protected TemplateModel createModel() throws TemplateModelException { 111 ServletContext servletContext = ServletActionContext 112 .getServletContext(); 113 HttpServletRequest request = ServletActionContext.getRequest(); 114 HttpServletResponse response = ServletActionContext.getResponse(); 115 ValueStack stack = ServletActionContext.getContext().getValueStack(); 116 Object action = null; 117 if (invocation != null) 118 action = invocation.getAction(); // Added for NullPointException 119 return freemarkerManager.buildTemplateModel(stack, action, 120 servletContext, request, response, wrapper); 121 } 122 protected Locale deduceLocale() { 123 if (invocation.getAction() instanceof LocaleProvider) { 124 return ((LocaleProvider) invocation.getAction()).getLocale(); 125 } else { 126 return configuration.getLocale(); 127 } 128 } 129 protected void postTemplateProcess(Template template, TemplateModel data) 130 throws IOException { 131 } 132 protected boolean preTemplateProcess(Template template, TemplateModel model) 133 throws IOException { 134 Object attrContentType = template.getCustomAttribute(”content_type”); 135 if (attrContentType != null) { 136 ServletActionContext.getResponse().setContentType( 137 attrContentType.toString()); 138 } else { 139 String contentType = getContentType(); 140 if (contentType == null) { 141 contentType = “text/html”; 142 } 143 String encoding = template.getEncoding(); 144 if (encoding != null) { 145 contentType = contentType + “; charset=” + encoding; 146 } 147 ServletActionContext.getResponse().setContentType(contentType); 148 } 149 return true; 150 } 151 public String getFileName() { 152 return fileName; 153 } 154 public void setFileName(String fileName) { 155 this.fileName = fileName; 156 } 157 public String getFilePath() { 158 return filePath; 159 } 160 public void setFilePath(String filePath) { 161 this.filePath = filePath; 162 } 163 public String getStaticTemplate() { 164 return staticTemplate; 165 } 166 public void setStaticTemplate(String staticTemplate) { 167 this.staticTemplate = staticTemplate; 168 } 169 }
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。
QQ:6203142
-----在北京的四川小伙