分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

beetl学习根据URL生成模板

发布时间:2023-09-06 02:14责任编辑:彭小芳关键词:暂无标签

官方文档: http://ibeetl.com/guide/#beetl

多谢beetl的作者抽空指点!!!

根据远程文件服务器生成模板:

需要注意的是:

  • StringTemplateResourceLoader:字符串模板加载器,用于加载字符串模板,如本例所示(根据url读取文件内容,然后根据这个对象生成Template对象)
  • FileResourceLoader:文件模板加载器,需要一个根目录作为参数构造,传入getTemplate方法的String是模板文件相对于Root目录的相对路径
  • ClasspathResourceLoader:文件模板加载器,模板文件位于Classpath里
  • WebAppResourceLoader:用于webapp集成,假定模板根目录就是WebRoot目录,参考web集成章
  • MapResourceLoader : 可以动态存入模板
  • CompositeResourceLoader 混合使用多种加载方式
protected void generateFile(String template, String filePath) { ???????if(template.indexOf("http") != -1) { ???????????try { ???????????????//创建一个URL实例 ???????????????URL url = new URL("http://xxx/xxx/wKgUFFub3CmAer4pAAABjPGvkcI576.btl"); ???????????????//通过URL的openStrean方法获取URL对象所表示的自愿字节输入流 ???????????????InputStream is = url.openStream(); ???????????????InputStreamReader isr = new InputStreamReader(is,"utf-8"); ???????????????//为字符输入流添加缓冲 ???????????????BufferedReader br = new BufferedReader(isr); ???????????????String data = null;//读取数据 ???????????????StringBuffer sb = new StringBuffer(); ???????????????while ((data = br.readLine())!=null){//循环读取数据 ???????????????????data += "\r\n"; ???????????????????sb.append(data); ???????????????} ???????????????br.close(); ???????????????isr.close(); ???????????????is.close(); ???????????????StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); ???????????????Configuration cfg = Configuration.defaultConfiguration(); ???????????????GroupTemplate gt = new GroupTemplate(resourceLoader,cfg); ???????????????Template pageTemplate = gt.getTemplate(sb.toString()); ???????????????templateToFile(filePath, pageTemplate); ???????????} catch (IOException e) { ???????????????e.printStackTrace(); ???????????} ???????}else { ???????????Template pageTemplate = groupTemplate.getTemplate(template); ???????????templateToFile(filePath, pageTemplate); ???????} ???} ???protected void templateToFile(String filePath, Template pageTemplate) { ???????configTemplate(pageTemplate); ???????if (PlatformUtil.isWindows()) { ???????????filePath = filePath.replaceAll("/+|\\\\+", "\\\\"); ???????} else { ???????????filePath = filePath.replaceAll("/+|\\\\+", "/"); ???????} ???????File file = new File(filePath); ???????File parentFile = file.getParentFile(); ???????if (!parentFile.exists()) { ???????????parentFile.mkdirs(); ???????} ???????FileOutputStream fileOutputStream = null; ???????try { ???????????fileOutputStream = new FileOutputStream(file); ???????????pageTemplate.renderTo(fileOutputStream); ???????} catch (FileNotFoundException e) { ???????????e.printStackTrace(); ???????} finally { ???????????try { ???????????????fileOutputStream.close(); ???????????} catch (IOException e) { ???????????????e.printStackTrace(); ???????????} ???????} ???}

beetl学习根据URL生成模板

原文地址:https://www.cnblogs.com/yxgmagic/p/9650414.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved