SpringMVC Java config freemarker

freemaker 默认的TemplateLoader是FileTemplateLoader所以:

 1     /**
 2      * 
 3      * Description:FreeMarker视图配置
 4      * @return 
 5      */
 6     @Bean
 7     public FreeMarkerViewResolver freeMarkerViewResolver() {
 8         FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
 9         viewResolver.setPrefix("/WEB-INF/ftl/");
10         viewResolver.setSuffix(".ftl");
11         viewResolver.setCache(true);
12         viewResolver.setContentType("text/html;charset=UTF-8");
13         viewResolver.setRequestContextAttribute("requestContext");
14         viewResolver.setOrder(0);
15         return viewResolver;
16     }

是找不到模板的:所以需要修改加载器为WebappTemplateLoader就可以了

 1     @Bean
 2     public FreeMarkerConfig freeMarkerConfig(){
 3         FreeMarkerConfigurer fmCfg=new FreeMarkerConfigurer();
 4         freemarker.template.Configuration tplCfg = new freemarker.template.Configuration();
 5         tplCfg.setTagSyntax(freemarker.template.Configuration.AUTO_DETECT_TAG_SYNTAX);
 6         tplCfg.setTemplateUpdateDelay(1);
 7         tplCfg.setDefaultEncoding("UTF-8");
 8         tplCfg.setOutputEncoding("UTF-8");
 9         tplCfg.setLocale(Locale.SIMPLIFIED_CHINESE);
10           tplCfg.setDateFormat("yyyy-MM-dd");
11         tplCfg.setTimeFormat("HH:mm:ss");
12         tplCfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
13         tplCfg.setClassicCompatible(true);//空串显示""
14         tplCfg.setTemplateLoader(new WebappTemplateLoader(servletContext));
15         fmCfg.setConfiguration(tplCfg);
16     
17         return fmCfg;
18     }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。