Java Code Examples for freemarker.template.Configuration#getVersion()

The following examples show how to use freemarker.template.Configuration#getVersion() . 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: MavenUtils.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
public static Template getTemplate(File templateFile) throws IOException {
    Configuration cfg = new Configuration(Configuration.getVersion());

    cfg.setTemplateLoader(new URLTemplateLoader() {
        @Override
        protected URL getURL(String name) {
            try {
                return new URL(name);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            }
        }
    });

    cfg.setDefaultEncoding("UTF-8");
    cfg.setLocalizedLookup(false);
    Template template = cfg.getTemplate(templateFile.toURI().toURL().toExternalForm());
    return template;
}
 
Example 2
Source File: FreemarkerTest.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateHtml() throws IOException, TemplateException {
    //创建配置类
    Configuration configuration = new Configuration(Configuration.getVersion());
    String classpath = this.getClass().getResource("/").getPath();
    //设置模板路径
    configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
    //设置字符集
    configuration.setDefaultEncoding("utf-8");
    //加载模板
    Template template = configuration.getTemplate("test1.ftl");
    //数据模型
    Map map = getMap();
    //静态化
    String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
    //静态化内容
    System.out.println(content);
    InputStream inputStream = IOUtils.toInputStream(content);
    //输出文件
    FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
    int copy = IOUtils.copy(inputStream, fileOutputStream);
}
 
Example 3
Source File: FreemarkerTest.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateHtmlByString() throws IOException, TemplateException {
    //创建配置类
    Configuration configuration = new Configuration(Configuration.getVersion());
    //获取模板内容
    //模板内容,这里测试时使用简单的字符串作为模板
    String templateString = "" +
            "<html>\n" +
            "    <head></head>\n" +
            "    <body>\n" +
            "    名称:${name}\n" +
            "    </body>\n" +
            "</html>";

    //加载模板
    //模板加载器
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    stringTemplateLoader.putTemplate("template", templateString);
    configuration.setTemplateLoader(stringTemplateLoader);
    Template template = configuration.getTemplate("template", "utf-8");

    //数据模型
    Map map = getMap();
    //静态化
    String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
    //静态化内容
    System.out.println(content);
    InputStream inputStream = IOUtils.toInputStream(content);
    //输出文件
    FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
    IOUtils.copy(inputStream, fileOutputStream);
}
 
Example 4
Source File: TemplateObjectMethods.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
private static BeansWrapper getBeansWrapper(){
	//创建builder:
    BeansWrapperBuilder builder = new BeansWrapperBuilder(Configuration.getVersion());
    // 设置所需的beanswrapper属性
    //builder.setUseModelCache(true);是否启用缓存
    //builder.setExposeFields(true);//是否启用返回类的公共实例。
    builder.setSimpleMapWrapper(true);//模板能使用Map方法
    // Get the singleton:
    BeansWrapper beansWrapper = builder.build();
	
	return beansWrapper;
}
 
Example 5
Source File: TemplateCustomMethods.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
private static BeansWrapper getBeansWrapper(){	
	//创建builder:
    BeansWrapperBuilder builder = new BeansWrapperBuilder(Configuration.getVersion());
    // 设置所需的beanswrapper属性
    //builder.setUseModelCache(true);是否启用缓存
    //builder.setExposeFields(true);//是否启用返回类的公共实例。
    builder.setSimpleMapWrapper(true);//模板能使用Map方法
    // Get the singleton:
    BeansWrapper beansWrapper = builder.build();
	
	return beansWrapper;
}
 
Example 6
Source File: FreemarkerResultHandler.java    From oxygen with Apache License 2.0 5 votes vote down vote up
FreemarkerResolver() {
  cfg = new Configuration(Configuration.getVersion());
  cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  cfg.setClassForTemplateLoading(Bootstrap.class,
      WebConfigKeys.VIEW_PREFIX_FREEMARKER.getValue());
  cfg.setNumberFormat(Strings.OCTOTHORP);
  if (WebConfigKeys.VIEW_CACHE.castValue(boolean.class)) {
    cfg.setCacheStorage(new MruCacheStorage(20, 250));
  } else {
    cfg.unsetCacheStorage();
  }
}
 
Example 7
Source File: TemplateParse.java    From genDoc with Apache License 2.0 5 votes vote down vote up
private TemplateParse(){
    configuration = new Configuration(Configuration.getVersion());
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    configuration.setTemplateLoader(stringTemplateLoader);
    configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.getVersion()));
    configuration.setDefaultEncoding("UTF-8");

    initTemplateCache();
}
 
Example 8
Source File: RuleCreator.java    From support-rulesengine with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws IOException {
  try {
    cfg = new Configuration(Configuration.getVersion());
    cfg.setDirectoryForTemplateLoading(new File(templateLocation));
    cfg.setDefaultEncoding(templateEncoding);
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  } catch (IOException e) {
    logger.error("Problem getting rule template location." + e.getMessage());
    throw e;
  }
}
 
Example 9
Source File: FreemarkerConfig.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	 * @param httpConfig
	 * @param root
	 * @return
	 * @throws IOException
	 */
	private Configuration createConfiguration(HttpConfig httpConfig, String root) throws IOException {
		if (httpConfig.getPageRoot() == null) {
			return null;
		}

		if (configurationCreater != null) {
			return configurationCreater.createConfiguration(httpConfig, root);
		}

		Configuration cfg = new Configuration(Configuration.getVersion());

		if (httpConfig.isPageInClasspath()) {
			cfg.setClassForTemplateLoading(this.getClass(), "/" + root/**.substring("classpath:".length())*/
			);
			//cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/template");
		} else {
			cfg.setDirectoryForTemplateLoading(new File(root));
		}
		cfg.setDefaultEncoding(httpConfig.getCharset());
		//		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		cfg.setLogTemplateExceptions(false);
		cfg.setWrapUncheckedExceptions(true);
		cfg.setTemplateExceptionHandler(ShortMessageTemplateExceptionHandler.me);
		cfg.setLocale(Locale.SIMPLIFIED_CHINESE);
		cfg.setNumberFormat("#");
//		cfg.setClassicCompatible(true);
		return cfg;
	}
 
Example 10
Source File: TemplateConfiguration.java    From opencps-v2 with GNU Affero General Public License v3.0 5 votes vote down vote up
private void init() {
	try {
		Configuration configuration = new Configuration(Configuration.getVersion());
		configuration.setDefaultEncoding("UTF-8");
		configuration.setObjectWrapper(new DefaultObjectWrapper(configuration.getIncompatibleImprovements()));

		this.setConfiguration(configuration);
	} catch (Exception e) {
		_log.error(e);
	}
}
 
Example 11
Source File: BaseDocumentationTest.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void loadTemplates() {
  loader = new ClassTemplateLoader(
      BaseDocumentationTest.class,
      "templates"
  );

  configuration = new Configuration(Configuration.getVersion());
  configuration.setDefaultEncoding("UTF-8");
  configuration.setTemplateLoader(loader);
  configuration.setObjectWrapper(new BeansWrapper(Configuration.getVersion()));
  configuration.setNumberFormat("computer");
}
 
Example 12
Source File: StructTemplate.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
public StructTemplate() {
  this.configuration = new Configuration(Configuration.getVersion());
  this.loader = new StringTemplateLoader();
  this.configuration.setTemplateLoader(this.loader);
  this.configuration.setDefaultEncoding("UTF-8");
  this.configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  this.configuration.setLogTemplateExceptions(false);
  this.configuration.setObjectWrapper(new ConnectObjectWrapper());
}
 
Example 13
Source File: TemplateConfiguration.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
private static Configuration createConfiguration() {
    final Configuration cfg = new Configuration(Configuration.getVersion());
    cfg.setClassForTemplateLoading(TemplateConfiguration.class, "/megamek/common/templates");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(false);
    cfg.setWrapUncheckedExceptions(true);
    return cfg;
}
 
Example 14
Source File: FreemarkerController.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
/**
 * 生成静态文件
 *
 * @throws IOException
 * @throws TemplateException
 */
public void generateHtml(String uid) {
    try {
        //创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        String classpath = this.getClass().getResource("/").getPath();
        //设置模板路径
        configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
        //设置字符集
        configuration.setDefaultEncoding("utf-8");
        //加载模板
        Template template = configuration.getTemplate("info.ftl");
        //数据模型
        Map map = new HashMap();

        List<Blog> sameBlog = blogService.getSameBlogByBlogUid(uid);
        sameBlog = setBlog(sameBlog);

        List<Blog> thirdBlog = blogService.getBlogListByLevel(ELevel.THIRD);
        thirdBlog = setBlog(thirdBlog);

        List<Blog> fourthBlog = blogService.getBlogListByLevel(ELevel.FOURTH);
        fourthBlog = setBlog(fourthBlog);

        map.put("vueWebBasePath", "http://localhost:9527/#/");
        map.put("webBasePath", "http://localhost:8603");
        map.put("staticBasePath", "http://localhost:8600");
        map.put("webConfig", webConfigService.getWebConfig());
        map.put("blog", blogService.getBlogByUid(uid));
        map.put("sameBlog", sameBlog);
        map.put("hotBlogList", blogService.getBlogListByTop(SysConf.FIVE));

        //静态化
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

        InputStream inputStream = IOUtils.toInputStream(content);
        //输出文件
        String savePath = fileUploadPath + "/blog/page/" + uid + ".html";
        FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
        int copy = IOUtils.copy(inputStream, fileOutputStream);
    } catch (Exception e) {
        e.getMessage();
    }

}
 
Example 15
Source File: MailTemplateProvider.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private MailTemplateProvider() {
    configuration = new Configuration(Configuration.getVersion());
    configuration.setClassForTemplateLoading(MailTemplateProvider.class, "/mail_templates");
    configuration.setDefaultEncoding("UTF-8");
}
 
Example 16
Source File: MailTemplateProvider.java    From kylin with Apache License 2.0 4 votes vote down vote up
private MailTemplateProvider() {
    configuration = new Configuration(Configuration.getVersion());
    configuration.setClassForTemplateLoading(MailTemplateProvider.class, "/mail_templates");
    configuration.setDefaultEncoding("UTF-8");
}