Java Code Examples for org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer#setDefaultEncoding()

The following examples show how to use org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer#setDefaultEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebMvcAutoConfiguration.java    From halo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Configuring freemarker template file path.
 *
 * @return new FreeMarkerConfigurer
 */
@Bean
public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) throws IOException, TemplateException {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/");
    configurer.setDefaultEncoding("UTF-8");

    Properties properties = new Properties();
    properties.setProperty("auto_import", "/common/macro/common_macro.ftl as common,/common/macro/global_macro.ftl as global");

    configurer.setFreemarkerSettings(properties);

    // Predefine configuration
    freemarker.template.Configuration configuration = configurer.createConfiguration();

    configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);

    if (haloProperties.isProductionEnv()) {
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }

    // Set predefined freemarker configuration
    configurer.setConfiguration(configuration);

    return configurer;
}
 
Example 2
Source File: MolgenisWebAppConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Configure freemarker. All freemarker templates should be on the classpath in a package called
 * 'freemarker'
 */
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
  FreeMarkerConfigurer result =
      new FreeMarkerConfigurer() {
        @Override
        protected void postProcessConfiguration(Configuration config) {
          config.setObjectWrapper(new MolgenisFreemarkerObjectWrapper(VERSION_2_3_23));
        }

        @Override
        protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) {
          templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, ""));
        }
      };
  result.setPreferFileSystemAccess(false);
  result.setTemplateLoaderPath("classpath:/templates/");
  result.setDefaultEncoding("UTF-8");
  Properties freemarkerSettings = new Properties();
  freemarkerSettings.setProperty(Configuration.LOCALIZED_LOOKUP_KEY, Boolean.FALSE.toString());
  freemarkerSettings.setProperty(Configuration.NUMBER_FORMAT_KEY, "computer");
  result.setFreemarkerSettings(freemarkerSettings);
  Map<String, Object> freemarkerVariables = Maps.newHashMap();
  freemarkerVariables.put("hasPermission", new HasPermissionDirective(permissionService));
  freemarkerVariables.put("notHasPermission", new NotHasPermissionDirective(permissionService));
  addFreemarkerVariables(freemarkerVariables);

  result.setFreemarkerVariables(freemarkerVariables);

  return result;
}