Velocity's first application example

黄舟
Release: 2023-03-04 21:22:02
Original
1155 people have browsed it

Velocity’s first application example

Steps to perform

/**
* 1.Initialize Velocity. This applies to bothusage patterns for Velocity,
* the Singleton as well as the 'separateruntime instance' (see more on
* this below), and you only do this once.

* 2. Create a Context object (more on whatthat is later).

* 3.Add your data objects to the Context.

* 4. Choose a template. 
* 5. 'Merge' the template and your data toproduce the
ouput.
* 
* @throws
Exception
*/

Velocity.init("src/velocity.properties");使用的是相对路径,项目下的src下的

//2. Create a Context object

VelocityContext context = newVelocityContext();

//3.Add your data objects to this context

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

context.put("who", "旗哥");

//4.Choose a template

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

StringWriter sw = new StringWriter();

//5.Merge the template and you data toproduce the output

template.merge(context, sw);

sw.flush();

System.out.println(sw.toString());
Copy after login

The above is the content of Velocity’s first application example. For more related information, please Follow the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!