java 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的實現。
package com.mobilesoft.esales.webapp.action; 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 javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.dispatcher.StrutsResultSupport; import org.apache.struts2.views.freemarker.FreemarkerManager; import org.apache.struts2.views.util.ResourceUtil; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.LocaleProvider; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.ValueStack; import freemarker.template.Configuration; import freemarker.template.ObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; public class FreemarkerResult extends StrutsResultSupport { private static final long serialVersionUID = -3778230771704661631L; protected ActionInvocation invocation; protected Configuration configuration; protected ObjectWrapper wrapper; protected FreemarkerManager freemarkerManager; private Writer writer; protected String location; private String pContentType = “text/html”; protected String fileName; // 要生成的靜態頁面名稱 protected String filePath; // 要生成的靜態頁面的路徑 protected String staticTemplate; // 用于生成靜態頁面Freemarker模板的路徑 public FreemarkerResult() { super(); } public FreemarkerResult(String location) { super(location); } @Inject public void setFreemarkerManager(FreemarkerManager mgr) { this.freemarkerManager = mgr; } public void setContentType(String aContentType) { pContentType = aContentType; } public String getContentType() { return pContentType; } public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException { this.location = location; this.invocation = invocation; this.configuration = getConfiguration(); this.wrapper = getObjectWrapper(); this.fileName = (String) conditionalParse(fileName, invocation); this.staticTemplate = (String) conditionalParse(staticTemplate, invocation); this.filePath = ((String) conditionalParse(filePath, invocation)) == null ? “” : ((String) conditionalParse(filePath, invocation)); if (!location.startsWith(”/”)) { ActionContext ctx = invocation.getInvocationContext(); HttpServletRequest req = (HttpServletRequest) ctx .get(ServletActionContext.HTTP_REQUEST); String base = ResourceUtil.getResourceBase(req); location = base + “/” + location; } //生成html頁面的模板類 Template template = configuration.getTemplate(location, deduceLocale()); // 生成靜態頁面的的模板類 Template staticTemplate = configuration.getTemplate(this.staticTemplate, deduceLocale()); TemplateModel model = createModel(); String path = ServletActionContext.getServletContext().getRealPath( filePath) + File.separator; Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(path + fileName))); if (preTemplateProcess(template, model)) { try { staticTemplate.process(model, out); template.process(model, getWriter()); } finally { postTemplateProcess(template, model); postTemplateProcess(staticTemplate, model); } } } protected Configuration getConfiguration() throws TemplateException { return freemarkerManager.getConfiguration(ServletActionContext .getServletContext()); } protected ObjectWrapper getObjectWrapper() { return configuration.getObjectWrapper(); } public void setWriter(Writer writer) { this.writer = writer; } protected Writer getWriter() throws IOException { if (writer != null) { return writer; } return ServletActionContext.getResponse().getWriter(); } protected TemplateModel createModel() throws TemplateModelException { ServletContext servletContext = ServletActionContext .getServletContext(); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ValueStack stack = ServletActionContext.getContext().getValueStack(); Object action = null; if (invocation != null) action = invocation.getAction(); // Added for NullPointException return freemarkerManager.buildTemplateModel(stack, action, servletContext, request, response, wrapper); } protected Locale deduceLocale() { if (invocation.getAction() instanceof LocaleProvider) { return ((LocaleProvider) invocation.getAction()).getLocale(); } else { return configuration.getLocale(); } } protected void postTemplateProcess(Template template, TemplateModel data) throws IOException { } protected boolean preTemplateProcess(Template template, TemplateModel model) throws IOException { Object attrContentType = template.getCustomAttribute(”content_type”); if (attrContentType != null) { ServletActionContext.getResponse().setContentType( attrContentType.toString()); } else { String contentType = getContentType(); if (contentType == null) { contentType = “text/html”; } String encoding = template.getEncoding(); if (encoding != null) { contentType = contentType + “; charset=” + encoding; } ServletActionContext.getResponse().setContentType(contentType); } return true; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getStaticTemplate() { return staticTemplate; } public void setStaticTemplate(String staticTemplate) { this.staticTemplate = staticTemplate; } }
本文由用戶 SimoneNewco 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!