Spring boot integrated freemarker detailed tutorial

We all know that spring boot is a service that simplifies configuration, saves cumbersome xml configuration, and replaces the previous xml configuration with properties, yml and annotations.

We first implement spring boot's support for freemarker and introduce pom

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

Add configuration items in yml:

spring:
    freemarker:
        template-loader-path: classpath:/ftl/

The classpath is followed by a prefix. It can also be implemented in the annotation configuration class

New configuration class: FreeMarkerConfig.java

import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModelException;
import java.io.IOException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
/**
 * @author zhuxiaomeng
 * @date 2018/1/2.
 * @email [email protected]
 */
@Configuration
public class FreeMarkerConfig {


  @Bean
  public ViewResolver viewResolver() {
    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setCache(false);
    resolver.setViewClass(org.springframework.web.servlet.view.freemarker.FreeMarkerView.class);
    resolver.setRequestContextAttribute("re");
    resolver.setExposeSpringMacroHelpers(true);
    resolver.setExposeRequestAttributes(true);
    resolver.setExposeSessionAttributes(true);
    resolver.setSuffix(".ftl");
    resolver.setContentType("text/html; charset=UTF-8");
    return resolver;
  }

  @Bean
  public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
    FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
    factory.setTemplateLoaderPaths("classpath:/ftl/", "src/main/resources/ftl");
    factory.setDefaultEncoding("UTF-8");
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();

    freemarker.template.Configuration configuration = factory.createConfiguration();
    configuration.setClassicCompatible(true);
    result.setConfiguration(configuration);
    Properties settings = new Properties();
    settings.put("template_update_delay", "0");
    settings.put("default_encoding", "UTF-8");
    settings.put("number_format", "0.##########");
    settings.put("datetime_format", "yyyy-MM-dd HH:mm:ss");
    settings.put("classic_compatible", true);
    settings.put("template_exception_handler", "ignore");
    result.setFreemarkerSettings(settings);
    return result;
  }

}

setRequestContextAttribute: Set the attribute name, which is the alias of the freemarker front-end RequestContext

I have implemented it in the open source framework lenos. If you are interested, or need help, you can download and learn:

Address: https://gitee.com/bweird/lenosp 

lenos is a rapid development scaffolding, not only has scheduled tasks, but also other technologies such as permission management, log monitoring, etc. If you like it, don't forget to click star, thank you. If you have any questions, you can ask in the group under lenos: 137738503.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324184342&siteId=291194637