当前位置: 代码迷 >> Web前端 >> Struts2跟Freemarker整合应用静态页面
  详细解决方案

Struts2跟Freemarker整合应用静态页面

热度:360   发布时间:2012-09-11 10:49:03.0
Struts2和Freemarker整合应用静态页面

0

踩 利用Struts2生成静态页面其实很灵活,很强大,尤其是利用Struts2对Freemarker较好的支持,充分利用Freemarker的模板功能来生成静态页面。
基本思路为:利用Struts2对自定义result type的支持,自定义能够生成静态页面的result type,结合模板引擎Freemarker可以实现大批量静态页面的生成。
参看org.apache.struts2.views.freemarker.FreemarkerResult的代码实现,自定义了自己的生成静态页 面的result type。此种方案不单纯用于生成静态页面,其实也可以用于生成诸如wml、xhtml等内容,具体可以参考Struts2缺省提供的各种result type的实现。
标签: FreeMarker web.xml Struts 代码片段(2)
[代码] FreemarkerResult.java
view sourceprint?
001 package com.mobilesoft.esales.webapp.action;  

002 import java.io.BufferedWriter; 

003 import java.io.File; 

004 import java.io.FileOutputStream; 

005 import java.io.IOException; 

006 import java.io.OutputStreamWriter; 

007 import java.io.Writer; 

008 import java.util.Locale;  

009 import javax.servlet.ServletContext; 

010 import javax.servlet.http.HttpServletRequest; 

011 import javax.servlet.http.HttpServletResponse;  

012 import org.apache.struts2.ServletActionContext; 

013 import org.apache.struts2.dispatcher.StrutsResultSupport; 

014 import org.apache.struts2.views.freemarker.FreemarkerManager; 

015 import org.apache.struts2.views.util.ResourceUtil;  

016 import com.opensymphony.xwork2.ActionContext; 

017 import com.opensymphony.xwork2.ActionInvocation; 

018 import com.opensymphony.xwork2.LocaleProvider; 

019 import com.opensymphony.xwork2.inject.Inject; 

020 import com.opensymphony.xwork2.util.ValueStack;  

021 import freemarker.template.Configuration; 

022 import freemarker.template.ObjectWrapper; 

023 import freemarker.template.Template; 

024 import freemarker.template.TemplateException; 

025 import freemarker.template.TemplateModel; 

026 import freemarker.template.TemplateModelException;  

027   

028 public class FreemarkerResult extends StrutsResultSupport {  

029     private static final long serialVersionUID = -3778230771704661631L;  

030     protected ActionInvocation invocation; 

031     protected Configuration configuration; 

032     protected ObjectWrapper wrapper; 

033     protected FreemarkerManager freemarkerManager; 

034     private Writer writer; 

035     protected String location; 

036     private String pContentType = “text/html”;  

037     protected String fileName; // 要生成的静态页面名称 

038     protected String filePath; // 要生成的静态页面的路径 

039     protected String staticTemplate; // 用于生成静态页面Freemarker模板的路径  

040     public FreemarkerResult() { 

041         super(); 

042     }  

043     public FreemarkerResult(String location) { 

044         super(location); 

045     }  

046     @Inject

047     public void setFreemarkerManager(FreemarkerManager mgr) { 

048         this.freemarkerManager = mgr; 

049     }  

050     public void setContentType(String aContentType) { 

051         pContentType = aContentType; 

052     }  

053     public String getContentType() { 

054         return pContentType; 

055     }  

056     public void doExecute(String location, ActionInvocation invocation) 

057             throws IOException, TemplateException { 

058         this.location = location; 

059         this.invocation = invocation; 

060         this.configuration = getConfiguration(); 

061         this.wrapper = getObjectWrapper();  

062         this.fileName = (String) conditionalParse(fileName, invocation); 

063         this.staticTemplate = (String) conditionalParse(staticTemplate, invocation); 

064         this.filePath = ((String) conditionalParse(filePath, invocation)) == null ? “” 

065                 : ((String) conditionalParse(filePath, invocation));  

066         if (!location.startsWith(”/”)) { 

067             ActionContext ctx = invocation.getInvocationContext(); 

068             HttpServletRequest req = (HttpServletRequest) ctx 

069                     .get(ServletActionContext.HTTP_REQUEST); 

070             String base = ResourceUtil.getResourceBase(req); 

071             location = base + “/” + location; 

072         }  

073         //生成html页面的模板类 

074         Template template = configuration.getTemplate(location, deduceLocale()); 

075         // 生成静态页面的的模板类 

076         Template staticTemplate = configuration.getTemplate(this.staticTemplate, 

077                 deduceLocale());  

078         TemplateModel model = createModel(); 

079         String path = ServletActionContext.getServletContext().getRealPath( 

080                 filePath) 

081                 + File.separator; 

082         Writer out = new BufferedWriter(new OutputStreamWriter( 

083                 new FileOutputStream(path + fileName)));  

084         if (preTemplateProcess(template, model)) { 

085             try { 

086                 staticTemplate.process(model, out); 

087                 template.process(model, getWriter()); 

088             } finally { 

089                 postTemplateProcess(template, model); 

090                 postTemplateProcess(staticTemplate, model); 

091             } 

092         } 

093     }  

094     protected Configuration getConfiguration() throws TemplateException { 

095         return freemarkerManager.getConfiguration(ServletActionContext 

096                 .getServletContext()); 

097     }  

098     protected ObjectWrapper getObjectWrapper() { 

099         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 }
[代码] struts.xml
view sourceprint?1 <action name=”staticViewAction”  class=”com.mobilesoft.esales.webapp.action.StaticViewtAction”> 

2     <result name=”success” type=”staticview”> 

3     <param name=”location”>test/freemarkertest.ftl</param> 

4     <param name=”contentType”>text/html</param> 

5      <param name=”fileName”>${filename}</param> 

6     <param name=”staticTemplate”>test/freemarkertest.ftl</param> 

7     <param name=”filePath”>static</param> 

8     </result>                     

9 </action>
  相关解决方案