velocity学习笔记

源文件地址:
Home Java 网络编程
Velocity笔记
周五, 2010年 01月 15日 09:09

“Velocity将Java代码从Web 页面中分离出来,使用Web站点从长远看更容易维护,并且提供了一种可行的JavaServer Pages替代解决方案。”——Velocity用户手册

Velocity下载地址:http://velocity.apache.org/

在开发中,我们必须导入\lib\log4j-1.2.12.jar和velocity-1.6-dep.jar两个包。如果使用velocity-1.6.jar会稍微麻烦一点,需要把\lib下的commons-collections-3.2.1.jar\commons-lang-2.4.jar和oro-2.0.8.jar放入类路径下。 velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。

导入开发包以后,我们需要在类路径中加入velocity.properties文档:

指定日志文件存放位置

runtime.log = E:\\spring\\velocity\\velocity_example.log

指定模版文件加载位置

file.resource.loader.path=E:\\spring\\velocity

指定输入编码格式

input.encoding=UTF-8

指定velocity的servlet向浏览器输出内容的编码

default.contentType=text/html; charset\=UTF-8

指定输出编码格式

output.encoding=UTF-8

接下来是老黎讲的10个例子:

一:

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("hello", "HelloWorld");

context.put("who", "黎明");

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

${who}说:${hello}

二:访问对象

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("person", new Person(2,"小张"));

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

${person.id}=${person.name}

三:历遍集合/数组

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("list", Arrays.asList("第一个","第二个","第三个"));

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach($element in $list)

$element

#end

四:历遍Map集合

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

Map<String,String> map = new HashMap<String, String>();

map.put("key1", "value1");

map.put("key2", "value2");

context.put("map", map);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach( $key in $map.keySet() )

$key=$map.get($key)

#end

五:获得当前迭代的索引

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

Map<String,String> map = new HashMap<String, String>();

map.put("key1", "value1");

map.put("key2", "value2");

context.put("map", map);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#foreach( $key in $map.keySet() )

$velocityCount > $key=$map.get($key)

#end

velocityCount 变量名可以通过directive.foreach.counter.name属性修改,如: directive.foreach.counter.name =index,以后可以通过$index进行访问。迭代的索引默认从1开始,我们可以通过directive.foreach.counter.initial.value=0进行修改。

六:在模版中进行赋值

#set( $name = "老张" )

$name


#set( $condition = true )


数组赋值:

#set( $ints = [1..10])

#foreach( $entry in $ints)

$entry

#end


#set( $ints = ["第一个","第二个"])

#foreach( $entry in $ints)

$entry

#end

七:#if#else#end

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("condition", true);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

#if($condition)

成立

#else

不成立

#end

八:格式化日期

格式化日期必须将velocity-tools-generic-1.4.jar包加入到类路径。

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("now", new Date());

context.put("dateformat", new DateTool());

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter sw = new StringWriter();

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

$dateformat.format("yyyy-MM-dd", $now)

九:输出到文件

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("title", "巴巴运行网");

context.put("body", "这是内容");

Template template = Velocity.getTemplate("mytemplate.vm");

File savefile = new File("E:\\spring\\velocity\\WebRoot\\page\\test.html");

if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();

FileOutputStream outstream = new FileOutputStream(savefile);

OutputStreamWriter writer = new OutputStreamWriter(outstream,"UTF-8");

BufferedWriter bufferWriter = new BufferedWriter(writer);

template.merge(context, bufferWriter);

bufferWriter.flush();

outstream.close();

bufferWriter.close();

}catch( Exception e ){

e.printStackTrace();

}

十:null处理

try{

Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();

context.put("title", null);

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter writer = new StringWriter();

BufferedWriter bufferWriter = new BufferedWriter(writer);

template.merge(context, bufferWriter);

bufferWriter.flush();

System.out.println(writer.toString());

}catch( Exception e ){

e.printStackTrace();

}

mytemplate.vm内容如下:

$title
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值