springboot+FreeMarker 乱码,注解方式

原文链接: https://my.oschina.net/u/989834/blog/1934462

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

@Configuration
public class FreeMarkerViewResolverConfig {

    @Bean(name = "viewResolver")
    public ViewResolver getViewResolver() {
        FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();

        viewResolver.setCache(true);
        viewResolver.setPrefix("/freemarker/");
        viewResolver.setSuffix(".ftl");
        viewResolver.setOrder(1);
        viewResolver.setContentType("text/html;charset=UTF-8");//我是因为这一条没有配置,导致乱码
        return viewResolver;
    }

    @Bean(name = "freemarkerConfig")
    public FreeMarkerConfigurer getFreemarkerConfig() {
        FreeMarkerConfigurer config = new FreeMarkerConfigurer();

        Properties properties = new Properties();
        properties.put("default_encoding", GlobalSetting.WEB_ENCODING);
        properties.put("output_encoding", GlobalSetting.WEB_ENCODING);
        properties.put("url_escaping_charset", GlobalSetting.WEB_ENCODING);
        properties.put("locale","zh_CN");
        properties.put("defaultEncoding", GlobalSetting.WEB_ENCODING);
        properties.put("date_format","yyyy-MM-dd");
        properties.put("time_format","HH:mm:ss");
        properties.put("datetime_format","yyyy-MM-dd HH:mm:ss");
        properties.put("classic_compatible","true");
        properties.put("template_exception_handler","rethrow");//#ignore,debug,html_debug,rethrow
        config.setFreemarkerSettings(properties);

// Folder containing FreeMarker templates.
// 1 - "/WEB-INF/views/"
// 2 - "classpath:/templates"
        config.setDefaultEncoding(GlobalSetting.WEB_ENCODING);
        config.setTemplateLoaderPath("classpath:/templates");

        return config;
    }

}

转载于:https://my.oschina.net/u/989834/blog/1934462

你可能感兴趣的:(springboot+FreeMarker 乱码,注解方式)