




已阅读5页,还剩55页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例 1.buildhtml.jsp % try String title=This is Title; String content=This is Content Area; String editer=LaoMao; String filePath = ; filePath = request.getRealPath(/)+test/template.htm; /out.print(filePath+); String templateContent=; FileInputStream fileinputstream = new FileInputStream(filePath);/读取模块文件 int lenght = fileinputstream.available(); byte bytes = new bytelenght; fileinputstream.read(bytes); fileinputstream.close(); templateContent = new String(bytes); /out.print(templateContent); templateContent=templateContent.replaceAll(#title#,title); templateContent=templateContent.replaceAll(#content#,content); templateContent=templateContent.replaceAll(#author#,editer);/替换掉模块中相应的地方 /out.print(templateContent); / 根据时间得文件名 Calendar calendar = Calendar.getInstance(); String fileame = String.valueOf(calendar.getTimeInMillis() +.html; fileame = request.getRealPath(/)+fileame;/生成的html文件保存路径 FileOutputStream fileoutputstream = new FileOutputStream(fileame);/建立文件输出流 byte tag_bytes = templateContent.getBytes(); fileoutputstream.write(tag_bytes); fileoutputstream.close(); catch(Exception e) out.print(e.toString(); % 2. template.htm #title# #title# author:#author# #content# 用freemarker模版生成静态html 2007-07-11 4:28 pm 睬一踩 | Rss订阅| 以前写过,不过忘记了。 参考/修改了网上的一个例子,代码如下:NewsItem .javapackage freemarker;/* author terrychan*/public class NewsItem public String title;public String addTime;public String showContent;public String getAddTime() return addTime;public void setAddTime(String addTime) this.addTime = addTime;public String getTitle() return title;public void setTitle(String title) this.title = title;public String getShowContent() return showContent;public void setShowContent(String showContent) this.showContent = showContent;public void loadData() this.title=”测试内容”; this.showContent=”测试freemarker生成静态页面”; this.addTime=”2007-09-10;FreeMarkerTest.javapackage freemarker;import java.io.*;import java.util.HashMap;import java.util.Map;import mons.logging.Log;import mons.logging.LogFactory;import freemarker.template.*;/* 测试FreeMarker.* author Terry.Chan*/public class FreeMarkerTest private final Log logger = LogFactory.getLog(getClass();private Configuration freemarker_cfg = null;static String sGeneFilePath = “d:test”;static String sGeneFileName = “freemarker.htm”;static String sTempPlateFilePath = “d:test”;public static void main(String args) throws IOException / todo 创建一个类,然后创建instanceNewsItem aItem = new NewsItem();aItem.loadData();FreeMarkerTest test = new FreeMarkerTest();Map root = new HashMap();root.put(”newsitem”, aItem);boolean bOK = test.geneHtmlFile(”template.ftl”, root, sGeneFilePath,sGeneFileName);/* * 获取freemarker的配置. freemarker本身支持classpath,目录和从ServletContext获取. */protected Configuration getFreeMarkerCFG() if (null = freemarker_cfg) / Initialize the FreeMarker configuration;/ - Create a configuration instancefreemarker_cfg = new Configuration();/ - FreeMarker支持多种模板装载方式,可以查看API文档,都很简单:路径,根据Servlet上下文,classpath等等/ htmlskin是放在classpath下的一个目录/ freemarker_cfg.setClassForTemplateLoading(this.getClass(),/ “/htmlskin”);/ freemarker_cfg.setTemplateLoader(arg0)/ freemarker_cfg.settry freemarker_cfg.setDirectoryForTemplateLoading(new File(sTempPlateFilePath); catch (Exception ex) ex.printStackTrace();return freemarker_cfg;/* * 生成静态文件. * * param templateFileName * 模板文件名,相对htmlskin路径,例如”/tpxw/view.ftl” * param propMap * 用于处理模板的属性Object映射 * param htmlFilePath * 要生成的静态文件的路径,相对设置中的根路径,例如 “/tpxw/1/2005/4/” * param htmlFileName * 要生成的文件名,例如 “1.htm” */public boolean geneHtmlFile(String templateFileName, Map propMap,String htmlFilePath, String htmlFileName) / todo 从配置中取得要静态文件存放的根路径:需要改为自己的属性类调用try Template t = getFreeMarkerCFG().getTemplate(templateFileName);/ 如果根路径存在,则递归创建子目录creatDirs(htmlFilePath);File afile = new File(htmlFilePath + “/” + htmlFileName);Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile);cess(propMap, out); catch (TemplateException e) logger.error(”Error while processing FreeMarker template ”+ templateFileName, e);return false; catch (IOException e) logger.error(”Error while generate Static Html File ”+ htmlFileName, e);return false;return true;/* * 创建多级目录 * * param aParentDir * String * param aSubDir * 以 / 开头 * return boolean 是否成功 */public static boolean creatDirs(String path) File aFile = new File(path);if (!aFile.exists() return aFile.mkdirs(); else return true;template.ftl查看文章: $newsitem.title $newsitem.title ( $newsitem.addTime ) 返回 $newsitem.showContent 查看文章:$ newsitem.title $ newsitem.title ($ newsitem.addtime ) 返回 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; $ newsitem.showContent 代码:import java.io. * ;import java.util.HashMap;import java.util.Map;import mons.logging.Log;import mons.logging.LogFactory;import freemarker.template. * ;/*/ /* *Createdon2005-4-7* */ /*/ /* *测试FreeMarker.* author scud* */ public class FreeMarkerTest private final Loglogger = LogFactory.getLog(getClass(); private Configurationfreemarker_cfg = null ; public static void main(Stringargs) / todo自己的一个类 NewsItemaItem = null ; / todo装入新闻 / NewsItem=loadNewsItem(1); FreeMarkerTesttest = new FreeMarkerTest();Maproot = new HashMap();root.put( newsitem ,aItem);StringsGeneFilePath = /tpxw/ ;StringsFileName = 1.htm ; boolean bOK = test.geneHtmlFile( /tpxw/view.ftl ,root,sGeneFilePath,sFileName); /*/ /* *获取freemarker的配置.freemarker本身支持classpath,目录和从ServletContext获取. */ protected ConfigurationgetFreeMarkerCFG() if ( null = freemarker_cfg) / InitializetheFreeMarkerconfiguration; / -Createaconfigurationinstance freemarker_cfg = new Configuration(); / -FreeMarker支持多种模板装载方式,可以查看API文档,都很简单:路径,根据Servlet上下文,classpath等等 / htmlskin是放在classpath下的一个目录 freemarker_cfg.setClassForTemplateLoading( this .getClass(), /htmlskin ); return freemarker_cfg; /*/ /* *生成静态文件.* param templateFileName模板文件名,相对htmlskin路径,例如/tpxw/view.ftl* param propMap用于处理模板的属性Object映射* param htmlFilePath要生成的静态文件的路径,相对设置中的根路径,例如/tpxw/1/2005/4/* param htmlFileName要生成的文件名,例如1.htm */ public boolean geneHtmlFile(StringtemplateFileName,MappropMap,StringhtmlFilePath,StringhtmlFileName) / todo从配置中取得要静态文件存放的根路径:需要改为自己的属性类调用 StringsRootDir = e:/webtest/htmlfile ; try Templatet = getFreeMarkerCFG().getTemplate(templateFileName); / 如果根路径存在,则递归创建子目录 creatDirs(sRootDir,htmlFilePath);Fileafile = new File(sRootDir + / + htmlFilePath + / + htmlFileName);Writerout = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(afile);cess(propMap,out); catch (TemplateExceptione) logger.error( ErrorwhileprocessingFreeMarkertemplate + templateFileName,e); return false ; catch (IOExceptione) logger.error( ErrorwhilegenerateStaticHtmlFile + htmlFileName,e); return false ; return true ; /*/ /* *创建多级目录* param aParentDirString* param aSubDir以/开头* return boolean是否成功 */ public static boolean creatDirs(StringaParentDir,StringaSubDir) FileaFile = new File(aParentDir); if (aFile.exists() FileaSubFile = new File(aParentDir + aSubDir); if ( ! aSubFile.exists() return aSubFile.mkdirs(); else return true ; else return false ; Hhhhhh漫步 | 漫步资源站Asp教程.Net教程PHP教程JSP教程数据库网页设计网络管理网站运作大杂烩 窗体顶端Web 窗体底端漫步统计系统2006个人版,降价了,每套仅需30元,详情请进. FreeMarker设计指南(1)JSP教程-Java技巧及代码 FreeMarker设计指南(1) 1、快速入门(1)模板 + 数据模型 = 输出l FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念l 他们是分工劳动的:设计者专注于表示创建HTML文件、图片、Web页面的其它可视化方面;程序员创建系统,生成设计页面要显示的数据l 经常会遇到的问题是:在Web页面(或其它类型的文档)中显示的信息在设计页面时是无效的,是基于动态数据的l 在这里,你可以在HTML(或其它要输出的文本)中加入一些特定指令,FreeMarker会在输出页面给最终用户时,用适当的数据替代这些代码l 下面是一个例子: Welcome! Welcome $user! Our latest product: $latestP! l 这个例子是在简单的HTML中加入了一些由$包围的特定代码,这些特定代码是FreeMarker的指令,而包含FreeMarker的指令的文件就称为模板(Template)l 至于user、latestProduct.url和latestP来自于数据模型(data model)l 数据模型由程序员编程来创建,向模板提供变化的信息,这些信息来自于数据库、文件,甚至于在程序中直接生成l 模板设计者不关心数据从那儿来,只知道使用已经建立的数据模型l 下面是一个可能的数据模型:(root) | +- user = Big Joe | +- latestProduct | +- url = products/greenmouse.html | +- name = green mousel 数据模型类似于计算机的文件系统,latestProduct可以看作是目录,而user、url和name看作是文件,url和name文件位于latestProduct目录中(这只是一个比喻,实际并不存在)l 当FreeMarker将上面的数据模型合并到模板中,就创建了下面的输出: Welcome! Welcome Big Joe! Our latest product: green mouse! (2)数据模型l 典型的数据模型是树型结构,可以任意复杂和深层次,如下面的例子:(root) | +- animals | | | +- mouse | | | | | +- size = small | | | | | +- price = 50 | | | +- elephant | | | | | +- size = large | | | | | +- price = 5000 | | | +- python | | | +- size = medium | | | +- price = 4999 | +- test = It is a test | +- whatnot | +- because = dont knowl 类似于目录的变量称为hashes,包含保存下级变量的唯一的查询名字l 类似于文件的变量称为scalars,保存单值l scalars保存的值有两种类型:字符串(用引号括起,可以是单引号或双引号)和数字(不要用引号将数字括起,这会作为字符串处理)l 对scalars的访问从root开始,各部分用“.”分隔,如animals.mouse.pricel 另外一种变量是sequences,和hashes类似,只是不使用变量名字,而使用数字索引,如下面的例子:(root) | +- animals | | | +- (1st) | | | | | +- name = mouse | | | | | +- size = small | | | | | +- price = 50 | | | +- (2nd) | | | | | +- name = elephant | | | | | +- size = large | | | | | +- price = 5000 | | | +- (3rd) | | | +- name = python | | | +- size = medium | | | +- price = 4999 | +- whatnot | +- fruits | +- (1st) = orange | +- (2nd) = bananal 这种对scalars的访问使用索引,如(3)模板l 在FreeMarker模板中可以包括下面三种特定部分: $:称为interpolations,FreeMarker会在输出时用实际值进行替代 FTL标记(FreeMarker模板语言标记):类似于HTML标记,为了与HTML标记区分,用#开始(有些以开始,在后面叙述) 注释:包含在(而不是)之间l 下面是一些使用指令的例子: if指令#if animals.python.price Pythons are cheaper than elephants today. Pythons are not cheaper than elephants today. list指令We have these animals: NamePrice $$being.price Euros 输出为:We have these animals: NamePrice mouse50 Euros elephant5000 Euros python4999 Euros include指令 Test page Test page Blah blah. 一起使用指令We have these animals: NamePrice $ $being.price Euros 点击此处收藏本文 发表于 2004年11月02日 1:23 AM -阿罡发表于2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 综合解析人教版9年级数学上册【旋转】重点解析试题(含答案解析版)
- 2026届四川省仁寿县英语九年级第一学期期末学业水平测试试题含解析
- 小讲解员礼仪培训
- 维泊妥珠单抗临床应用解析
- 2026届山东省东营市东营区胜利一中学化学九上期中学业质量监测模拟试题含解析
- 2026届安康市重点中学化学九年级第一学期期末监测模拟试题含解析
- 湖北省枣阳市太平一中学2026届九年级化学第一学期期中考试模拟试题含解析
- 一日流程环节培训
- 河南省开封市田家炳实验中学2026届九上化学期中监测模拟试题含解析
- 湖北利川文斗2026届英语九上期末考试模拟试题含解析
- 《提升思维高度:战略思维培养与应用》课件
- 教官聘用合同协议
- DB63T 1599-2025 高海拔高寒地区公路边坡生态防护技术设计规范
- 企业孵化贸易增量合同样本
- Module 9 great inventions Unit 3 教学设计 2024-2025学年外研版九年级英语上册
- 统编版小学语文五年级上册第三单元快乐读书吧《中国民间故事》导读课课件
- 钢材采购知识培训课件
- SketchUp 教程-第1章 初识 SketchUp
- 兰州交通大学《高等数学》测验题库及参考答案
- 2024年中考语文真题分类汇编-教师版-专题10 小说阅读
- 大便常规临床意义
评论
0/150
提交评论