- 浏览: 930684 次
- 性别:
- 来自: 山西
文章分类
最新评论
-
白小默:
你好 可以提供下源码DEMO吗,不知为何,我导出来的excel ...
jxls 使用模板文件导出生成excel -
zkzqzzz:
博主威武!
让微信二维码扫描您的APK -
zkzqzzz:
感谢博主 原来那些类都不是必须的 或者自己写!!博主真棒 ...
抢红包插件实现原理浅析 -
zkzqzzz:
博主 请问你的其他类在哪里呢?
抢红包插件实现原理浅析 -
zkzqzzz:
其他类在哪呢?
抢红包插件实现原理浅析
-
-
在Struts中有两种使用Velocity的方法,一种是利用Velocity的vm模板进行页面展示,一种则是利用Velocity来生成静态页面。以下介绍在Struts 1.*版本中使用Velocity模板生成静态页面的过程。
思路是访问一个Action,在Action中进行静态页面的生成,最终该Action跳转到生成好的静态页面中。
步骤为:
1. 获取VelocityContext,该对象中包含了需要展示的数据
2. 获取指定路径下的vm模板内容
3. 根据Velocity模板,生成字符串
4. 根据指定路径及文件名创建文件
5. 将转换好的模板文件写入指定文件中
源码如下:
Velocity模板
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>StrutsSample_1</title>
</head>
<body>
<h3>StrutsSample</h3>
<table width="400" border="1">
#foreach($name in $list)
<tr> <td>$name</td> </tr>
#end
</table>
</body>
</html>
public class CreateHtmlPage extends DispatchAction {
public ActionForward createStaticPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward forward = new ActionForward();
ActionMessages errors = new ActionMessages();
String webServer_dir = "D:/Tomcat6/webapps/Mixele_Velocity";
String filepath = webServer_dir + "/success.htm"; //需要生成的静态页
String vmpath = webServer_dir + "/templates/namelist.vm"; //Velocity模板
VelocityContext context = getContext();
String templatebody = getTemplateBody(vmpath);
String str = createHtmlStrByVelocity(context, templatebody);
File htmlfile = createFile(filepath);
writerTxtFile(htmlfile, str);
if (!errors.isEmpty()) {
saveErrors(request, errors);
forward = new ActionForward(mapping.getInput());
} else {
forward = new ActionForward("/success.htm");
}
return forward;
}
/** 获取VelocityContext对象,并向其中注入模板需要展现的数据 */
public VelocityContext getContext() {
/* 这里需要注意,因为在web.xml中配置了VelocityViewServlet
* 所以这里Velocity会在系统启动时初始化
* 如果没有配置,需要先在这里进行Velocity的初始化工作 */
VelocityContext context = new VelocityContext();
List<String> list = new ArrayList<String>();
String name = null;
for(int i=0;i<20;i++){
name = "Hoffman YoYo "+i+" Name";
list.add(name);
}
context.put("list", list);
return context;
}
/** 通过指定路径获取vm模板并提取模板内容 */
public String getTemplateBody(String vmpath) {
File file = new File(vmpath);
BufferedReader reader = null;
StringBuffer strbf = new StringBuffer("");
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
strbf.append(tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return strbf.toString();
}
/** 根据Velocity模板,生成字符串 */
public String createHtmlStrByVelocity(VelocityContext context, String vmbody) {
StringWriter writer = new StringWriter();
try {
Velocity.evaluate(context, writer, "myString", vmbody);
} catch (ParseErrorException e) {
e.printStackTrace();
} catch (MethodInvocationException e) {
e.printStackTrace();
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return writer.toString();
}
/** 在指定路径中创建由Velocity解析出的html文件
* 当文件存在时,删除该文件,重新创建 */
public File createFile(String filepath) {
File file = new File(filepath);
if(file.exists() && file.isFile()) {
boolean delFileSuccess = file.delete(); //文件存在删除之
if(!delFileSuccess) {
System.out.println("删除文件" + filepath + "失败!");
}
try {
boolean cresteFileSuccess = file.createNewFile();
if (!cresteFileSuccess) {
System.out.println("创建文件" + filepath + "失败!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/** 写文本文件 */
public void writerTxtFile(File filepath, String str) {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(filepath), 1024));
} catch (IOException e) {
e.printStackTrace();
}
out.println(str);
out.flush();
out.close();
}
}
通过以上的代码,即可将赋予VelocityContext中的数据与VM模板组合,生成静态页面。
以上代码很多地方都是硬编码,其实在实际工程中,静态化的步骤不会发生太大变化,只是模板与生成页面的路径会有更复杂的获取过程。
文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/3_program/java/javajs/20100719/456479.html
发表评论
-
java通过sftp JSch 上传文件下载文件查看文件目录,测试可用
2019-12-19 18:19 879基于maven ... -
服务器之间的 zip 文件定时传送
2019-12-19 10:28 4751、expect 安装 将expect和tcl的软 ... -
Java/web/jsp根据pdf模板生成荣誉证书PDF文件
2019-07-19 14:48 8541.前言 最近博主在 ... -
Java生成荣誉证书PDF文件
2019-07-19 13:08 1274Java生成荣誉证书PD ... -
百度云API刷脸
2019-07-13 11:41 579刷脸登录是基于人工智能、生物识别、3D传感、大数据风控技术, ... -
maven--maven配置多个源文件夹
2019-06-13 21:32 855需求 Maven 为我们提供了一致的项目目录配置(源文件 ... -
绿盟检测出“检测到目标URL存在http host头攻击漏洞”如何解决
2019-06-09 10:00 868绿盟检测出“检测到目标URL存在http host头攻击漏 ... -
Linux安装apache及其简单的反向代理配置
2019-06-07 09:06 559Apache简介 Apache HTTP Se ... -
Linux二进制安装apache2.4.25
2019-06-07 09:06 660Linux二进制安装apache2.4. ... -
weblogic配置https,http自动跳转转https,ssl
2019-05-21 09:44 847最近,公司要求将http ... -
bootstrap-table组合表头
2019-03-06 10:04 848bootstrap-table组合表头 ... -
[Weblogic]如何清理缓存
2019-03-04 15:23 808[Weblogic]如何清理缓存 ... -
Guns第十节Swagger的讲解
2019-01-23 16:51 5422018年08月01日 15:54:30 ze ... -
Anaconda详细安装使用教程
2019-01-22 15:07 565关注微信公众号【Mi ... -
Windows系统下Eclipse上搭建Python开发环境
2019-01-22 15:00 285Windows系统下Eclipse上搭 ... -
Python 3.6 中使用pdfminer解析pdf文件
2019-01-22 14:50 887所使用python环境为最新 ... -
Python提取PDF内容(文本、图像、线条等)
2019-01-22 14:43 7492使用Python抽取PDF文件内 ... -
用python解析pdf中的文本与表格【pdfplumber的安装与使用】
2019-01-22 14:40 1474我们接触到的很多文档资料都是以pdf格式存在的,比如:论文, ... -
java实现PDF转HTML
2019-01-21 10:14 849java实现PDF转HTML 问题场景: ... -
JAVA PDFBOX 读取PDF表格
2019-01-18 17:39 2608最近在帮公司做工具,需要读取PDF中表格的数据。网上查了, ...
相关推荐
赠送jar包:velocity-engine-core-2.3.jar; 赠送原API文档:velocity-engine-core-2.3-javadoc.jar; 赠送源代码:velocity-engine-core-2.3-sources.jar; 赠送Maven依赖信息文件:velocity-engine-core-2.3.pom;...
赠送jar包:velocity-engine-core-2.3.jar 赠送原API文档:velocity-engine-core-2.3-javadoc.jar 赠送源代码:velocity-engine-core-2.3-sources.jar 包含翻译后的API文档:velocity-engine-core-2.3-javadoc-...
赠送jar包:velocity-tools-generic-3.1.jar; 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar; 赠送源代码:velocity-tools-generic-3.1-sources.jar; 赠送Maven依赖信息文件:velocity-tools-generic-...
velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar
velocity-engine-core-2.1.jar
赠送jar包:velocity-tools-generic-3.1.jar 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar 赠送源代码:velocity-tools-generic-3.1-sources.jar 包含翻译后的API文档:velocity-tools-generic-3.1-...
struts2-struts1-plugin-2.3.30.jar, struts2-testng-plugin-2.3.30.jar, struts2-tiles-plugin-2.3.30.jar, struts2-tiles3-plugin-2.3.30.jar, tiles-api-2.2.2.jar, tiles-compat-3.0.1.jar, tiles-core-2.2.2....
还在为找不到jar文件烦心吗,不用了到我空间来有你想要的,持续更新。。。 velocity-1.5.jar,velocity-1.6.2-dep.jar,velocity-tools-1.3.jar
velocity 2.0 包需要的 jar 包 官方下载的,测试过了,可以运行 Demo 网上都有,关键几行代码: // ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // 按文件加载 // ve.init(); // Template t = ...
velocity-dep-1.4.jar velocity-dep-1.4.jar
velocity-tools-view-1.4.jar
velocity-tools-view-2.0.jar
velocity-1.7-dep.jar.zip velocity-1.7-dep.jar.zip velocity-1.7-dep.jar.zip
• commons-collections-3.2.jar • velocity-1.4.jar • velocity-tools-view-1.2.jar • avalon-logkit-2.1.jar
velocity-tools-generic-2.0.jar
velocity-tools-view-2.0-beta1.jar, 供大家使用
velocity-tools-1.4.jar
api-1.0.6.jar,tiles-request-freemarker-1.0.6.jar,tiles-request-jsp-1.0.6.jar,tiles-request-servlet-1.0.6.jar,tiles-servlet-3.0.7.jar,tiles-template-3.0.7.jar,validation-api-1.1.0.Final.jar,velocity-...
赠送jar包:velocity-1.7.jar; 赠送原API文档:velocity-1.7-javadoc.jar; 赠送源代码:velocity-1.7-sources.jar; 包含翻译后的API文档:velocity-1.7-javadoc-API文档-中文(简体)版.zip 对应Maven信息:...
velocity-tools-generic-1.2.jar