Velocity模板引擎的介绍和基本的模板语言语法使用
来源:互联网 发布:php从哪里接私活 编辑:程序博客网 时间:2024/06/11 08:59
类似于PHP中的Smarty,Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。从而实现界面和Java代码的分离,使得界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点。
另外,Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。
从http://velocity.apache.org/网站上下载最新的Velocity,这里我们下载了velocity-1.7.zip
解压velocity-1.7.zip,发下其根目录下有两个JAR文件:
velocity-1.7.jar velocity-1.7-dep.jar
其中velocity-1.7-dep.jar包含了:
velocity-1.7.jar commons-collections-3.2.1.jar commons-lang-2.4.jar oro-2.0.8.jar(这些JAR文件位于解压目录的lib目录下)
在JAR包不冲突的情况下可以直接使用velocity-1.7-dep.jar
该文件一般包含如下配置:
runtime.log = F:\project\MusicalInstrumentsStore\velocity_example.logfile.resource.loader.path = F:\project\MusicalInstrumentsStore\vminput.encoding = UTF-8output.encoding = UTF-8
runtime.log指定日志文件存放位置
file.resource.loader.path指定模板的加载位置
input.encoding指定输入编码
output.encoding指定输出编码
//初始化模板引擎Velocity.init("src/velocity.properties");//获取VelocityContextVelocityContext context = new VelocityContext();//为Context设置变量context.put("title", "HelloWorld");context.put("author", "arthinking");//获取模板文件Template template = Velocity.getTemplate("helloworld.vm");StringWriter sw = new StringWriter();//使用模板文件的merge函数合并模板和context提供的变量,输出到StringWriter中template.merge(context, sw);sw.flush();System.out.println(sw.toString());
${who}${content}
运行Java文件,使用Velocity生成的信息就打印出来了。
和使用EL表达式差不多,直接使用”.”导航。
如访问object对象的id属性:${object.id }
#foreach($element in $list)#element#end
#if($condition)true#elsefalse#end
默认使用变量名:$velocityCount
也可以自定义此变量名,在velocity.properties中设置:
directive.foreach.counter.name=index
设置索引起始位置为0:
directive.foreach.counter.initial.value=0
#foreach($key in $map.keySet())$key : $map.get($key)#end
#set(#a=”Hello World!”)$a#set($array1=[1..10])#foreach($entry in $array1)#entry#end
//初始化模板引擎Velocity.init("src/velocity.properties");//获取VelocityContextVelocityContext context = new VelocityContext();//为Context设置变量context.put("content", "HelloWorld");context.put("who", "arthinking");//获取模板文件Template template = Velocity.getTemplate("helloworld.vm");//创建输出文件File output = new File("D:/Velocity/1.html");if(!output.getParentFile().exists())output.getParentFile().mkdir();//创建输出流FileOutputStream outputStream = new FileOutputStream(output);OutputStreamWriter writer = new OutputStreamWriter(outputStream);BufferedWriter bufferedWriter = new BufferedWriter(writer);template.merge(context, bufferedWriter);bufferedWriter.flush();outputStream.close();bufferedWriter.close();
--------------------------------------------------------------
例子:
@SuppressWarnings("serial")public class VelocityTest extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();Properties pro = new Properties();InputStream input = this.getClass().getClassLoader().getResourceAsStream("velocity.properties");pro.load(input);input.close();Velocity.init(pro);VelocityContext context = new VelocityContext();context.put("title", "HelloWorld");List<Student> students = new ArrayList<Student>();for(int i = 0;i < 20;i++){Student student = new Student();student.setName("student"+i);students.add(student);}context.put("author", "arthinking");context.put("students", students);//获取模板文件Template template = Velocity.getTemplate("main.vm");StringWriter writer = new StringWriter();template.merge(context, writer);writer.flush();out.write(writer.toString());}}
模板文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>$title</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript" src = "/MyBlog/resource/scripts/jquery-1.8.3.js"></script> </head> <body><ul> #foreach($stu in $students)<li>$stu.name</li>#end</ul> </body></html>
输出:
- Velocity模板引擎的介绍和基本的模板语言语法使用
- velocity模板引擎基本语法
- 一种基于java语言的模板引擎velocity的使用
- Java模板引擎Velocity基本语法
- Velocity模板引擎语法
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- velocity模板的使用简单的语法
- Velocity 模板引擎介绍
- Velocity 模板引擎介绍
- Velocity 模板引擎介绍
- Velocity 模板引擎介绍
- velocity模板引擎的基本语法(个人实际编写的例子)
- Smarty模板引擎基本语法的学习
- PHP 模板引擎Smarty的基本语法
- smarty模板引擎的基本语法
- Velocity模板引擎基础语法
- window API 精准耗时计算
- js中的数组和字符串的一些方法
- html包含另外html页面的方法
- Oracle中TO_DATE格式
- 单例模式
- Velocity模板引擎的介绍和基本的模板语言语法使用
- ASN.1笔记——标准编码规则BER
- IOS-ScrollView创造Android-ViewPager效果
- [oracle@localhost ~]$ emctl start dbconsole OC4J Configuration issue. /home/oracle/app/oracle/produc
- 正则表达式:匹配符号
- ListView自适应实现表格
- 我的算法学习之路
- 获得sql server的table的表结构 -- 转到word中
- 将list的内容倒序