0

0

Velocity练习:使用vm模板生成最简单的html页面_html/css_WEB-ITnose

php中文网

php中文网

发布时间:2016-06-21 08:48:53

|

2830人浏览过

|

来源于php中文网

原创

这几天要用到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模板转换工具类,代码如下:

Kagi Search
Kagi Search

Kagi是一个注重隐私、以用户为中心的搜索引擎。

下载
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", "file:///C:/Users/Tsybius/Desktop/kju.jpg");            HashMap<String, String> hashMapContact = new HashMap<String, String>();            hashMapContact.put("Tel", "XXX-XXXXXXXX");            hashMapContact.put("Fax", "XXX-XXXXXXXX");            hashMapContact.put("Mobile", "XXX-XXXX-XXXX");            context.put("contactItems", hashMapContact);            Vector<String> vectorRemark = new Vector<String>();            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")<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />  <style type="text/css">  </style>  <head>    <title>$!{name}</title>  </head>  <body>    <table width="100%">      <tr>        <!-- Title -->        <td align="center"><b>Address Book - $!{name}</b></td>      </tr>      <tr>        <!-- BaseInfo -->        <td>          <table width="100%" height="100%">            <tr>              <td width="10%">Name:</td>              <td width="30%">$!{name}</td>              <td width="10%">Gender:</td>              <td width="30%">$!{gender}</td>              <td width="20%" rowspan="4">                <img  src="$!{portraitPath}"    style="max-width:90%"  style="max-width:90%"/ alt="Velocity练习:使用vm模板生成最简单的html页面_html/css_WEB-ITnose" >              </td>            </tr>            <tr>              <td width="10%">E-mail:</td>              <td width="30%">$!{email}</td>              <td width="10%">Job:</td>              <td width="30%">$!{job}</td>            </tr>            <tr>              <td width="10%">Company:</td>              <td width="70%" colspan="3">$!{company}</td>            </tr>            <tr>              <td width="10%">Address:</td>              <td width="70%" colspan="3">$!{address}</td>            </tr>          </table>        </td>      </tr>      <tr>        <!-- Contact -->        <td>          <table width="100%" height="100%" >            <tr>              <td width="10%" align="left" valign="top">                Contact:              </td>              <td>                <table width="100%" height="100%" >                  <tr>                    <th></th>                    <th align="left">Contact Type</th>                    <th align="left">Contact Code</th>                    <th></th>                  </tr>                  #foreach($contactItem in $contactItems.entrySet())                    <tr>                      <td align="left" width="5%">$velocityCount</td>                      <td align="left" width="20%">$contactItem.key</td>                      <td align="left" width="30%">$contactItem.value</td>                      <td></td>                    </tr>                  #end                 </table>              </td>            </tr>          </table>        </td>      </tr>      <tr>        <!-- Remarks-->        <td>          <table width="100%" height="100%" >            <tr>              <td width="10%" align="left" valign="top">                Remark:              </td>              <td>                <table width="100%" height="100%" >                  #foreach($remark in $remarks)                    <tr>                      <td>[$velocityCount] $remark</td>                    </tr>                  #end                 </table>              </td>            </tr>          </table>        </td>      </tr>    </table>  </body></html>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />  <style type="text/css">  </style>  <head>    <title>Kim Jung-un</title>  </head>  <body>    <table width="100%">      <tr>        <!-- Title -->        <td align="center"><b>Address Book - Kim Jung-un</b></td>      </tr>      <tr>        <!-- BaseInfo -->        <td>          <table width="100%" height="100%">            <tr>              <td width="10%">Name:</td>              <td width="30%">Kim Jung-un</td>              <td width="10%">Gender:</td>              <td width="30%">Male</td>              <td width="20%" rowspan="4">                <img  src="file:///C:/Users/Tsybius/Desktop/kju.jpg"    style="max-width:90%"  style="max-width:90%"/ alt="Velocity练习:使用vm模板生成最简单的html页面_html/css_WEB-ITnose" >              </td>            </tr>            <tr>              <td width="10%">E-mail:</td>              <td width="30%">kim001@northkorea.gov</td>              <td width="10%">Job:</td>              <td width="30%">Chairman of the WPK</td>            </tr>            <tr>              <td width="10%">Company:</td>              <td width="70%" colspan="3">Workers' Party Of Korea (WPK)</td>            </tr>            <tr>              <td width="10%">Address:</td>              <td width="70%" colspan="3">The Dark Side Laboratory, Pyongyang, North Korea</td>            </tr>          </table>        </td>      </tr>      <tr>        <!-- Contact -->        <td>          <table width="100%" height="100%" >            <tr>              <td width="10%" align="left" valign="top">                Contact:              </td>              <td>                <table width="100%" height="100%" >                  <tr>                    <th></th>                    <th align="left">Contact Type</th>                    <th align="left">Contact Code</th>                    <th></th>                  </tr>                    <tr>                      <td align="left" width="5%">1</td>                      <td align="left" width="20%">Tel</td>                      <td align="left" width="30%">XXX-XXXXXXXX</td>                      <td></td>                    </tr>                    <tr>                      <td align="left" width="5%">2</td>                      <td align="left" width="20%">Fax</td>                      <td align="left" width="30%">XXX-XXXXXXXX</td>                      <td></td>                    </tr>                    <tr>                      <td align="left" width="5%">3</td>                      <td align="left" width="20%">Mobile</td>                      <td align="left" width="30%">XXX-XXXX-XXXX</td>                      <td></td>                    </tr>                  </table>              </td>            </tr>          </table>        </td>      </tr>      <tr>        <!-- Remarks-->        <td>          <table width="100%" height="100%" >            <tr>              <td width="10%" align="left" valign="top">                Remark:              </td>              <td>                <table width="100%" height="100%" >                  <tr>                    <td>[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.</td>                  </tr>                  <tr>                    <td>[2] Kim is the son of Kim Jong-il (1941–2011) and the grandson of Kim Il-sung (1912–1994).</td>                  </tr>                  <tr>                    <td>[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.</td>                  </tr>                  <tr>                    <td>[4] Kim was named the World's 46th Most Powerful Person by the Forbes list of The World's Most Powerful People in 2013</td>                  </tr>                </table>              </td>            </tr>          </table>        </td>      </tr>    </table>  </body></html>

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

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

END

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

28

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

23

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

27

2026.02.28

Golang 并发编程模型与工程实践:从语言特性到系统性能
Golang 并发编程模型与工程实践:从语言特性到系统性能

本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。

16

2026.02.27

Golang 高级特性与最佳实践:提升代码艺术
Golang 高级特性与最佳实践:提升代码艺术

本专题深入剖析 Golang 的高级特性与工程级最佳实践,涵盖并发模型、内存管理、接口设计与错误处理策略。通过真实场景与代码对比,引导从“可运行”走向“高质量”,帮助构建高性能、可扩展、易维护的优雅 Go 代码体系。

18

2026.02.27

Golang 测试与调试专题:确保代码可靠性
Golang 测试与调试专题:确保代码可靠性

本专题聚焦 Golang 的测试与调试体系,系统讲解单元测试、表驱动测试、基准测试与覆盖率分析方法,并深入剖析调试工具与常见问题定位思路。通过实践示例,引导建立可验证、可回归的工程习惯,从而持续提升代码可靠性与可维护性。

2

2026.02.27

漫蛙app官网链接入口
漫蛙app官网链接入口

漫蛙App官网提供多条稳定入口,包括 https://manwa.me、https

164

2026.02.27

deepseek在线提问
deepseek在线提问

本合集汇总了DeepSeek在线提问技巧与免登录使用入口,助你快速上手AI对话、写作、分析等功能。阅读专题下面的文章了解更多详细内容。

8

2026.02.27

AO3官网直接进入
AO3官网直接进入

AO3官网最新入口合集,汇总2026年可用官方及镜像链接,助你快速稳定访问Archive of Our Own平台。阅读专题下面的文章了解更多详细内容。

309

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
前端基础进阶-移动Web
前端基础进阶-移动Web

共187课时 | 24.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号