下面列出了怎么用org.springframework.web.servlet.view.freemarker.FreeMarkerConfig的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
@ConditionalOnMissingBean({FreeMarkerConfig.class, FreeMarkerViewResolver.class})
public FreeMarkerConfigurer freeMarkerConfigurer() {
PluginFreeMarkerConfigurer configurer = new PluginFreeMarkerConfigurer();
applyProperties(configurer);
String[] paths = this.properties.getTemplateLoaderPath();
// paths = ArrayUtils.add(paths, WEBFTLS_PATH);
configurer.setTemplateLoaderPaths(paths);
List<WithAnnotationBeanData<FreeMarkerViewTools>> tools = SpringUtils.getBeansWithAnnotation(applicationContext, FreeMarkerViewTools.class);
tools.forEach(t->{
String name = t.getAnnotation().value();
if(StringUtils.isBlank(name)){
name = t.getBean().getClass().getSimpleName();
}
configurer.setFreemarkerVariable(name, t.getBean());
logger.info("registered FreeMarkerViewTools : {}", name);
});
return configurer;
}
/**
* 自动检测FreeMarkerConfig
*
* @return
* @throws BeansException
*/
protected FreeMarkerConfig autodetectConfiguration() throws BeansException {
try {
return (FreeMarkerConfig) BeanFactoryUtils
.beanOfTypeIncludingAncestors(getApplicationContext(),
FreeMarkerConfig.class, true, false);
} catch (NoSuchBeanDefinitionException ex) {
throw new ApplicationContextException(
"Must define a single FreeMarkerConfig bean in this web application context "
+ "(may be inherited): FreeMarkerConfigurer is the usual implementation. "
+ "This bean may be given any name.", ex);
}
}
/**
* Instead of returning the same bean from the application context, a {@link FreeMarkerConfig} is returned for
* the current {@link SiteContext}.
*/
@Override
protected FreeMarkerConfig autodetectConfiguration() throws BeansException {
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
return siteContext.getFreeMarkerConfig();
} else {
return super.autodetectConfiguration();
}
}
public ObjectFactory<FreeMarkerConfig> getFreeMarkerConfig() {
return freeMarkerConfig;
}
public void setFreeMarkerConfig(ObjectFactory<FreeMarkerConfig> freeMarkerConfig) {
this.freeMarkerConfig = freeMarkerConfig;
}
public ObjectFactory<FreeMarkerConfig> getFreeMarkerConfig() {
return freeMarkerConfig;
}
public void setFreeMarkerConfig(ObjectFactory<FreeMarkerConfig> freeMarkerConfig) {
this.freeMarkerConfig = freeMarkerConfig;
}
@Required
public void setFreeMarkerConfigFactory(final ObjectFactory<FreeMarkerConfig> freeMarkerConfigFactory) {
this.freeMarkerConfigFactory = freeMarkerConfigFactory;
}
public FreeMarkerConfig getFreeMarkerConfig() {
return freeMarkerConfig;
}
public void setFreeMarkerConfig(FreeMarkerConfig freeMarkerConfig) {
this.freeMarkerConfig = freeMarkerConfig;
}
@Required
public void setFreeMarkerConfigFactory(ObjectFactory<FreeMarkerConfig> freeMarkerConfigFactory) {
this.freeMarkerConfigFactory = freeMarkerConfigFactory;
}
/**
* Invoked on startup. Looks for a single FreeMarkerConfig bean to find the
* relevant Configuration for this factory.
* <p>
* Checks that the template for the default Locale can be found: FreeMarker
* will check non-Locale-specific templates if a locale-specific one is not
* found.
*
* @see freemarker.cache.TemplateCache#getTemplate
*/
protected void initApplicationContext() throws BeansException {
super.initApplicationContext();
if (getConfiguration() == null) {
FreeMarkerConfig config = autodetectConfiguration();
setConfiguration(config.getConfiguration());
}
checkTemplate();
}