struts2 自定义FreemarkerManager

原freemarker配置的ftl读取路径是默认在classes下,
增加userConfiguration配置使其可以在web路径下读取ftl

Java代码   收藏代码
  1. package net.esj.basic.plugins.freemarker;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import javax.servlet.ServletContext;  
  7.   
  8. import net.esj.basic.utils.ApplicationBeanContext;  
  9.   
  10. import org.apache.struts2.views.freemarker.FreemarkerManager;  
  11. import org.springframework.context.annotation.Scope;  
  12. import org.springframework.stereotype.Component;  
  13. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  14.   
  15. import freemarker.template.Configuration;  
  16. import freemarker.template.TemplateException;  
  17.   
  18. /** 
  19.  * 将struts的freemarkerManager配置放置在spring容器中维护 
  20.  * @author Administrator 
  21.  * 
  22.  */  
  23. @Component("springConfigToFreemarkerManager")  
  24. @Scope("prototype")  
  25. public class SpringConfigToFreemarkerManager extends FreemarkerManager {  
  26.       
  27.     private static final String SPRING_FREEMARKER_BEAN = "freemarkerConfig";  
  28.   
  29.     private Configuration userConfiguration;  
  30.       
  31.     @Override    
  32.     protected Configuration createConfiguration(ServletContext servletContext)     
  33.             throws TemplateException {     
  34.         FreeMarkerConfigurer fmconfig = (FreeMarkerConfigurer) ApplicationBeanContext.getBean(SPRING_FREEMARKER_BEAN);  
  35.         try {  
  36.             Configuration configuration =fmconfig.createConfiguration();  
  37.             configuration.setWhitespaceStripping(true);  
  38.             return  configuration;  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }     
  42.         return super.createConfiguration(servletContext);  
  43.     }     
  44.   
  45.     protected Configuration createUserConfiguration(ServletContext servletContext) throws TemplateException{  
  46.         userConfiguration =createConfiguration(servletContext);  
  47.           
  48.         String realpath = servletContext.getRealPath("/");  
  49.           
  50.         try {  
  51.             userConfiguration.setDirectoryForTemplateLoading(new File(realpath));  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.         return userConfiguration;  
  56.     }  
  57.   
  58.     public Configuration getUserConfiguration(ServletContext servletContext) {  
  59.         if(userConfiguration==null){  
  60.             try {  
  61.                 createUserConfiguration(servletContext);  
  62.             } catch (TemplateException e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.         }  
  66.         return userConfiguration;  
  67.     }  
  68. }  

struts.properties文件中
struts.freemarker.manager.classname= net.esj.basic.plugins.freemarker.SpringConfigToFreemarkerManager
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值