如何为Spring MVC webapp添加Freemarker支持?依赖问题?

时间:2013-07-13 11:33:46

标签: spring spring-mvc freemarker

我正在尝试使用Freemarker和Java配置创建Spring MVC应用程序。

我习惯使用JSP,但我想在这个项目中给Freemarker一个旋转。

我添加了Freemarker作为依赖,并且Maven正在下载jar:

        <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.20</version>
    </dependency>

我正在尝试使用3.2.3.RELEASE版本的Spring。

我的MVC配置,通过阅读类似问题组装而成:

@ComponentScan(basePackages="yhj.*")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{


@Bean
public ViewResolver viewResolver() {
    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setCache(true);
    resolver.setPrefix("");
    resolver.setSuffix(".ftl");
    return resolver;

}

@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    result.setTemplateLoaderPath("/WEB-INF/views/");
    return result;

}
}

当我尝试运行时,我收到编译错误,我的IDE告诉我'setTemplateLoaderPath(String)'方法无法解析。

所以...看起来像是使用Freemarker配置Spring MVC的正确方法吗?如果这是问题,我需要将哪些依赖项添加到我的POM中?

4 个答案:

答案 0 :(得分:14)

我的Spring Boot 1.0.2,Spring 4.0.3和FreeMarker 2.3.20的解决方案是以下类:

@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
  @Bean
  public ViewResolver viewResolver() {
    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setCache(true);
    resolver.setPrefix("");
    resolver.setSuffix(".ftl");
    resolver.setContentType("text/html; charset=UTF-8");
    return resolver;
  }

  @Bean
  public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
    FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
    factory.setTemplateLoaderPath("classpath:templates");
    factory.setDefaultEncoding("UTF-8");
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    result.setConfiguration(factory.createConfiguration());
    return result;
  }
}

此外,您需要在问题和DaFoot自己的答案中进行相同的pom.xml更改。

答案 1 :(得分:11)

我已经对编译问题进行了排序,为我的pom添加了一个依赖...

    <dependency><!-- needed for freemarker FreeMarkerConfigurer stuff -->
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>

答案 2 :(得分:5)

@Bean(name = "freeMarkerViewResolver")
    public FreeMarkerViewResolver getFreeMarkerViewResolver() { 
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setPrefix("/WEB-INF/views/json/");
        resolver.setSuffix(".json");
        resolver.setOrder(2);
        resolver.setContentType("application/json");
        resolver.setCache(true);
        return resolver;
    }

    @Bean(name = "freemarkerConfig")
    public FreeMarkerConfigurer getFreemarkerConfig() throws IOException, TemplateException { 
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setTemplateLoaderPaths("/"); // prevents FreeMarkerConfigurer from using its default path allowing setPrefix to work as expected
        return result;
    }

答案 3 :(得分:4)

这是我使用Boot

的MVC项目的自定义配置
In [96]: triang.rvs(c=0.5, loc=-1, scale=2, size=10)
Out[96]: 
array([-0.61654942,  0.03949263,  0.44191603, -0.76464285, -0.5474533 ,
        0.00343265,  0.222072  , -0.14161595,  0.46505966, -0.23557379])

前2个加载器允许从“/ WEB-INF / ftl”的/** * Otras configuraciones de la aplicaciones web, incluyendo algunas definidas en * xml. Usar @ImportResource("classpath:/extra-config.xml") en caso de quererse * importar configuracion en xml */ @Configuration @PropertySource("classpath:application.properties") public class WebAppConfig { @Autowired private ServletContext context; @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer placeHolderConfigurer = new PropertySourcesPlaceholderConfigurer(); return placeHolderConfigurer; } @Bean public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer() { @Override protected void postProcessConfiguration(freemarker.template.Configuration config) throws IOException, TemplateException { WebappTemplateLoader WebAppTplLoader = new WebappTemplateLoader(context, "/WEB-INF/ftl"); ClassTemplateLoader classTplLoader = new ClassTemplateLoader(context.getClassLoader(), "/templates"); ClassTemplateLoader baseMvcTplLoader = new ClassTemplateLoader(FreeMarkerConfigurer.class, ""); MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] { WebAppTplLoader, classTplLoader, baseMvcTplLoader }); config.setTemplateLoader(mtl); } }; configurer.setDefaultEncoding("UTF-8"); configurer.setPreferFileSystemAccess(false); return configurer; } @Bean public FreeMarkerViewResolver viewResolver() { FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); viewResolver.setExposeSpringMacroHelpers(true); viewResolver.setExposeRequestAttributes(true); viewResolver.setPrefix(""); viewResolver.setSuffix(".ftl"); viewResolver.setContentType("text/html;charset=UTF-8"); return viewResolver; } } 个文件和.ftl的常规war文件中加载jar个模板。 如果你想在freemarker中使用安全标签,那么escense就是这两行:

src/resources/templates

viewResolver.setExposeSpringMacroHelpers(true); viewResolver.setExposeRequestAttributes(true); 加载程序从baseMvcTplLoader获取spring.ftl。我建议在某些example projectdocumentation中探索ftl模板,以了解org.springframework.web.servlet.view.freemarker的工作原理。

  

占位符的配置与freemarker无关   配置,但它对于在变量中注入值非常有用   使用spring.ftlsrc/resources/application.properties开始   注释

使用此功能,您可以使用 freemarker 模板中的所有 spring 功能。