Configuration configuration = freeMarkerConfigurer.getConfiguration(); 无法获取到模板
时间: 2023-11-16 13:58:52 浏览: 82
可能是因为你没有正确配置 FreeMarker 的模板文件路径。你可以在 FreeMarker 的配置中设置模板文件的路径,示例代码如下:
```java
@Configuration
public class FreeMarkerConfig {
@Autowired
private ApplicationContext applicationContext;
@Bean(name = "freeMarkerConfig")
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates/");
Properties settings = new Properties();
settings.setProperty("defaultEncoding", "UTF-8");
settings.setProperty("number_format", "0.######");
settings.setProperty("boolean_format", "true,false");
settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
configurer.setFreemarkerSettings(settings);
return configurer;
}
@Bean(name = "freeMarkerViewResolver")
public FreeMarkerViewResolver freeMarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setOrder(1);
resolver.setCache(false);
resolver.setContentType("text/html;charset=UTF-8");
resolver.setRequestContextAttribute("request");
resolver.setSuffix(".ftl");
resolver.setViewClass(FreeMarkerView.class);
return resolver;
}
@Bean
public FreeMarkerView freeMarkerView() {
FreeMarkerView view = new FreeMarkerView();
view.setExposeSpringMacroHelpers(true);
view.setExposeRequestAttributes(true);
view.setExposeSessionAttributes(true);
view.setRequestContextAttribute("request");
return view;
}
}
```
在上面的代码中,`setTemplateLoaderPath()` 方法设置了模板文件的路径为 `classpath:/templates/`,你可以根据实际情况修改为你的模板文件路径。如果你的模板文件是放在 `src/main/resources/templates/` 目录下,那么可以将 `setTemplateLoaderPath()` 方法的参数改为 `"classpath:/templates/"`。
阅读全文