视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Velocity练习:使用vm模板生成最简单的html页面_html/css
2020-11-27 16:39:45 责编:小采
文档
这几天要用到Velocity模板引擎去做一些页面,所以学习了下这个工具。我的jdk版本为 1.8.0_25

需要做的准备工作有:

1、建立一个Java工程,需要引用Velocity相关的jar包,这些jar包可从apache.org下载到最新版本。 下载地址在:http://velocity.apache.org/download.cgi 。我下载的文件是velocity-tools-2.0.zip,将这些文件导入到项目中,就可以使用Velocity进行编码了。

2、安装Eclipse上的Velocity插件Veloedit。因为googlecode被gfw墙掉了,没有条件的朋友们可以安装网上下载到的离线安装版。这里还有一个需要注意,我的Eclipse版本为 Luna Service Release 1 (4.4.1),这个版本必须要先利用Eclipse自带的update功能,下载“Elipse Tests, Examples, and Extras”。

建立一个JavaProject,取名VelocityTool,里面新建两个java文件。

其中,VelocityHelper.java包含一个Velocity模板转换工具类,代码如下:

import java.io.File;import java.io.FileWriter;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.app.VelocityEngine;/** * * @文件名称 VelocityHelper.java * @文件作者 Tsybius2014 * @创建时间 2016年5月10日 下午2:14:01 */public class VelocityHelper { /** * * @param inputVmFilePath * @param outputHtmlFilePath * @param context * @throws Exception */ public static void generateHtml(String inputVmFilePath, String outputHtmlFilePath, VelocityContext context) throws Exception { try { Velocity.init(); VelocityEngine engine = new VelocityEngine(); Template template = engine.getTemplate(inputVmFilePath, "gbk"); File outputFile = new File(outputHtmlFilePath); FileWriter writer = new FileWriter(outputFile); template.merge(context, writer); writer.close(); } catch (Exception ex) { throw ex; } }}

VelocityTool.java中有main函数,代码如下:

import java.util.HashMap;import java.util.Vector;import org.apache.velocity.VelocityContext;/** * * @文件名称 VelocityTool.java * @文件作者 Tsybius2014 * @创建时间 2016年5月10日 上午10:03:59 */public class VelocityTool { public static void main(String[] args) { try { VelocityContext context= new VelocityContext(); context.put("name", "Kim Jung-un"); context.put("gender", "Male"); context.put("email", "XXX@XXX.gov"); context.put("job", "Chairman of the WPK"); context.put("company", "Workers' Party Of Korea (WPK)"); context.put("address", "XXXXXXXXX, Pyongyang, North Korea"); context.put("portraitPath", "https://www.gxlcms.com/file:///C:/Users/Tsybius/Desktop/kju.jpg"); HashMap hashMapContact = new HashMap(); hashMapContact.put("Tel", "XXX-XXXXXXXX"); hashMapContact.put("Fax", "XXX-XXXXXXXX"); hashMapContact.put("Mobile", "XXX-XXXX-XXXX"); context.put("contactItems", hashMapContact); Vector vectorRemark = new Vector(); vectorRemark.add("Kim Jong-un is the Chairman of the Workers' Party of Korea and supreme leader of the Democratic People's Republic of Korea (DPRK), commonly referred to as North Korea."); vectorRemark.add("Kim is the son of Kim Jong-il (1941–2011) and the grandson of Kim Il-sung (1912–1994)."); vectorRemark.add("Kim obtained two degrees, one in Physics at Kim Il-sung University, and another as an Army officer at the Kim Il-sung Military University."); vectorRemark.add("Kim was named the World's 46th Most Powerful Person by the Forbes list of The World's Most Powerful People in 2013"); context.put("remarks", vectorRemark); VelocityHelper.generateHtml( "velocity_test.vm", "C:\\Users\\Tsybius\\Desktop\\output.html", context); System.out.println("生成完毕"); } catch (Exception ex) { ex.printStackTrace(); } }}

模板velocity_test.vm是一个存储着人物通讯录的模板,代码如下:

#set($docDesc="VelocityTest")#set($docAuthor="Tsybius2014")#set($docDateTime="2016-05-10 14:29:04")#set($docRemark="none")    $!{name}   
Address Book - $!{name}
Name: $!{name} Gender: $!{gender}
E-mail: $!{email} Job: $!{job}
Company: $!{company}
Address: $!{address}
Contact: #foreach($contactItem in $contactItems.entrySet()) #end
Contact Type Contact Code
$velocityCount $contactItem.key $contactItem.value
Remark: #foreach($remark in $remarks) #end
[$velocityCount] $remark

以JavaApplication的方式运行本工程后,生成的HTML页面output.html如下:

    Kim Jung-un   
Address Book - Kim Jung-un
Name: Kim Jung-un Gender: Male
E-mail: kim001@northkorea.gov Job: Chairman of the WPK
Company: Workers' Party Of Korea (WPK)
Address: The Dark Side Laboratory, Pyongyang, North Korea
Contact:
Contact Type Contact Code
1 Tel XXX-XXXXXXXX
2 Fax XXX-XXXXXXXX
3 Mobile XXX-XXXX-XXXX
Remark:
[1] Kim Jong-un is the Chairman of the Workers' Party of Korea and supreme leader of the Democratic People's Republic of Korea (DPRK), commonly referred to as North Korea.
[2] Kim is the son of Kim Jong-il (1941–2011) and the grandson of Kim Il-sung (1912–1994).
[3] Kim obtained two degrees, one in Physics at Kim Il-sung University, and another as an Army officer at the Kim Il-sung Military University.
[4] Kim was named the World's 46th Most Powerful Person by the Forbes list of The World's Most Powerful People in 2013

使用 Firefox 46.0.1 打开此页面后效果图如下:

(部分内容摘自对应人物的维基百科页面)

END

下载本文
显示全文
专题