`

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>
分享到:
评论

相关推荐

    Struts2整合Freemarker生成静态页面

    NULL 博文链接:https://depravedangel.iteye.com/blog/443575

    struts2+freemarker 框架

    Struts2整合FreeMarker实例框架初学例子....利用Struts2生成静态页面其实很灵活,很强大,尤其是利用Struts2对Freemarker较好的支持,充分利用Freemarker的模板功能来生成静态页面。 比较简单.大家见笑了.

    FreeMarker与struts2整合生成静态页面

    FreeMarker与struts2整合,一个小案例实现【增删改查】生成静态页面。内含数据库sql文件及效果图。数据库是mysql,c3p0+dbUtils实现数据操作。是一个完整的案例。

    freemarker+struts2+spring完美整合!

    部署到tomcat中, 访问/freemarker/build_index.action 点击首页生成,当显示生成成功过后 然后访问 ...已经完美将struts2+freemarker+spring整合~ 希望对你们有所帮助。 经测试:tomcat5.5 无法正常运行

    Struts+Spring+Hibernate+Freemarker新闻系统

    能生成静态的新闻页面可以供Struts+Spring+Hibernate+Freemarker框架的整合应用!

    Struts2属性文件详解

    该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts 2内建的FreeMarker管理器. struts.freemarker.wrapper.altMap 该属性只支持true和false两个属性值,默认值是true.通常无需修改...

    Java Web程序设计教程

    7.3.2struts2整合freemarker 166 7.3.3使用struts2标签设计模板 170 7.4freemarker与velocity的比较 171 本章小结 171 课后练习 172 第8章hibernate框架基础 173 8.1orm简介 173 8.1.1应用orm的意义 173 ...

    ssh整合项目

    采用优化性能技术,采用oscache缓存,freemarker静态页面生成; 功能比较多但操作简单的后台管理;(后台管理附图片); 安装方法: 1:mysql中新建数据库dgssen.导入dgssen.sql 也可以修改:WebRoot\META-INF\context....

    spring in action英文版

     3.3.3 使用Spring的静态切入点  3.3.4 使用动态切入点  3.3.5 切入点实施  3.4 创建引入  3.4.1 实现IntroductionInterceptor  3.4.2 创建一个引入Advisor  3.4.3 谨慎使用引入通知  3.5 ...

    java开发常用jar包

    Sitemesh 是一个基于WEB页面的布局、装饰以及应用整合的开源框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的 banner,一致的版权,等等。它不仅仅能处理动态的内容,...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.2.1. 创建SqlMap 12.5.2.2. 使用 SqlMapTemplate 和 SqlMapDaoSupport 12.5.3. iBATIS SQL Maps 2.x 12.5.3.1. 创建SqlMapClient 12.5.3.2....

    Beetl模板引擎-其他

    而且消耗较低的CPU4、易于整合:Beetl能很容易的与各种web框架整合,如Spring MVC,JFinal,Struts,Nutz,Jodd,Servlet等。5、支持模板单独开发和测试,即在MVC架构中,即使没有M和C部分,也能开发和测试模板。6、...

Global site tag (gtag.js) - Google Analytics