protected static FreeMarkerConfigurer getFreeMarkerConfigurer(
      ApplicationContext applicationContext) {
    Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
    freemarkerVariables.put("xml_escape", "fmXmlEscape");
    freemarkerVariables.put("replaceParam", new ReplaceParamTemplateMethod());
    freemarkerVariables.put("timeAgo", new TimeAgoTemplateMethod());
    freemarkerVariables.putAll(listTemplateMethod(applicationContext));

    Properties freemarkerSettings = new Properties();
    freemarkerSettings.put("template_update_delay", "1");
    freemarkerSettings.put("defaultEncoding", "UTF-8");

    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("/WEB-INF/ftl/");
    configurer.setFreemarkerVariables(freemarkerVariables);
    configurer.setFreemarkerSettings(freemarkerSettings);

    try {
      configurer.afterPropertiesSet();
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    } catch (TemplateException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    return configurer;
  }
예제 #2
0
 @Bean
 public FreeMarkerConfigurer freemarkerConfig() {
   FreeMarkerConfigurer result =
       new org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer();
   result.setTemplateLoaderPath("/WEB-INF/views/ftl/");
   return result;
 }
예제 #3
0
  /**
   * Configure freemarker. All freemarker templates should be on the classpath in a package called
   * 'freemarker'
   */
  @Bean
  public FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    result.setPreferFileSystemAccess(false);
    result.setTemplateLoaderPath("classpath:/templates/");

    return result;
  }
예제 #4
0
 @Bean(name = "freemarkerConfig")
 public FreeMarkerConfigurer freemarkerConfig() {
   FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
   configurer.setTemplateLoaderPath("/WEB-INF/templates/");
   Map<String, Object> map = new HashMap<>();
   map.put("xml_escape", new XmlEscape());
   configurer.setFreemarkerVariables(map);
   return configurer;
 }
 private static void check() {
   if (configuration == null) {
     FreeMarkerConfigurer freeMarkerConfigurer = SpringUtils.getBean("freemarkerConfigurer");
     if (freeMarkerConfigurer != null) {
       configuration = freeMarkerConfigurer.getConfiguration();
     } else {
       ExceptionUtils.throwRuntimeException("spring has not freemarkerConfigurer");
     }
   }
 }
  @Bean
  public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freemarkerConfig = new FreeMarkerConfigurer();
    freemarkerConfig.setTemplateLoaderPath("classpath:/templates");
    freemarkerConfig.setDefaultEncoding("UTF-8");

    Map<String, Object> freemarkerVariables = new HashMap<>();
    freemarkerVariables.put("layout", freemarkerLayoutDirectives());

    freemarkerConfig.setFreemarkerVariables(freemarkerVariables);
    return freemarkerConfig;
  }
예제 #7
0
 /** * 整合freemarket */
 @Bean
 public FreeMarkerConfig freeMarkerConfig() {
   FreeMarkerConfigurer freeMarkerConfig = new FreeMarkerConfigurer();
   freeMarkerConfig.setTemplateLoaderPath("classpath:/view/");
   Properties settings = new Properties();
   settings.setProperty("template_update_delay", "1");
   settings.setProperty("defaultEncoding", "utf-8");
   settings.setProperty("url_escaping_charset", "utf-8");
   settings.setProperty("locale", "zh_CN");
   freeMarkerConfig.setFreemarkerSettings(settings);
   return freeMarkerConfig;
 }
예제 #8
0
 /**
  * 解析字符串模板
  *
  * @param template 字符串模板
  * @param model 数据
  * @return 解析后内容
  */
 public static String process(String template, Map<String, ?> model)
     throws IOException, TemplateException {
   Configuration configuration = null;
   ApplicationContext applicationContext = SpringUtils.getApplicationContext();
   if (applicationContext != null) {
     FreeMarkerConfigurer freeMarkerConfigurer =
         SpringUtils.getBean("freeMarkerConfigurer", FreeMarkerConfigurer.class);
     if (freeMarkerConfigurer != null) {
       configuration = freeMarkerConfigurer.getConfiguration();
     }
   }
   return process(template, model, configuration);
 }
예제 #9
0
 protected String buildText(JobExecution job) {
   Map<String, Object> result = getExecutionResult(job);
   try {
     fmConfg.setTemplateLoaderPath(config.getProperty("batch.freemarkDir"));
     Template t = fmConfg.getConfiguration().getTemplate(messageTemplate);
     return FreeMarkerTemplateUtils.processTemplateIntoString(t, result);
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return new StringBuffer("<html><body>")
       .append(result.toString().replace(",", ",<br/>"))
       .append("</body></html>")
       .toString();
 }
예제 #10
0
  @Transactional(readOnly = true)
  public int build(String templatePath, String staticPath, Map<String, Object> model) {
    Assert.hasText(templatePath);
    Assert.hasText(staticPath);

    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    Writer writer = null;
    try {
      freemarker.template.Template template =
          freeMarkerConfigurer.getConfiguration().getTemplate(templatePath);
      File staticFile = new File(servletContext.getRealPath(staticPath));
      File staticDirectory = staticFile.getParentFile();
      if (!staticDirectory.exists()) {
        staticDirectory.mkdirs();
      }
      fileOutputStream = new FileOutputStream(staticFile);
      outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
      writer = new BufferedWriter(outputStreamWriter);
      template.process(model, writer);
      writer.flush();
      return 1;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      IOUtils.closeQuietly(writer);
      IOUtils.closeQuietly(outputStreamWriter);
      IOUtils.closeQuietly(fileOutputStream);
    }
    return 0;
  }
예제 #11
0
  /**
   * @param tpl
   * @param params
   * @return
   * @throws Exception
   */
  private String getHtmlTextFromTemplate(String tpl, Map<String, Object> params) throws Exception {

    // 通过指定模板名获取FreeMarker模板实例
    Template fm = freeMarker.getConfiguration().getTemplate(tpl);

    // FreeMarker通过Map传递动态数据
    return FreeMarkerTemplateUtils.processTemplateIntoString(fm, params);
  }
예제 #12
0
  /**
   * form기반으로 페이징이 처리되는 html을 얻는다.
   *
   * @param formName
   * @return
   */
  public String getPageHtml(String formName) {

    this.setFormName(formName);

    String resultHtml = "";

    StringWriter stringWriter = null;
    try {
      FreeMarkerConfigurer config =
          SpringUtils.getBean("freemarkerConfig", FreeMarkerConfigurer.class);
      stringWriter = new StringWriter();
      Template template =
          config.getConfiguration().getTemplate(ConfigUtils.getString("global.page.template"));

      Map<String, Object> data = new HashMap<String, Object>();
      data.put("page", this);

      template.process(data, stringWriter);

      resultHtml = stringWriter.toString();

    } catch (Exception ignore) {
      logger.error(ignore.getMessage(), ignore);
      // nothing...
    } finally {
      try {
        if (stringWriter != null) {
          stringWriter.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return resultHtml;
  }
예제 #13
0
 private String createCache(
     String templatePath, String path, ModelMap model, HttpServletResponse response) {
   int time = verify(templatePath);
   if (0 != time) {
     String htmlPath = getHtmlFilePath(basePath, path);
     if (check(htmlPath, time)) {
       return cacheFileDirectory + path;
     } else {
       response.setCharacterEncoding("UTF-8");
       try {
         FreeMarkerUtils.makeFileByFile(
             getTemplateFilePath(templatePath),
             htmlPath,
             freeMarkerConfigurer.getConfiguration(),
             model);
         return cacheFileDirectory + path;
       } catch (Exception e) {
         return templatePath;
       }
     }
   } else {
     return templatePath;
   }
 }
예제 #14
0
 /**
  * 获取freemarker的配置,freemarker本身支持classpath,目录或从ServletContext获取.
  *
  * @return Configuration 返回freemaker的配置属性
  * @throws Exception
  */
 private Template getFreeMarkerCFG(String templateName) throws Exception {
   Configuration configuration = freemarkerConfig.getConfiguration();
   return configuration.getTemplate(templateName);
 }
 @Override
 public void afterPropertiesSet() throws IOException, TemplateException {
   super.afterPropertiesSet();
   this.getConfiguration().setSharedVariable("shiro", new ShiroTags());
 }
예제 #16
0
 public void clearTemplateCache() {
   freeMarkerConfigurer.getConfiguration().clearTemplateCache();
 }