
Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.
When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a site that looks good, and programmers can focus solely on writing top-notch code. Velocity separates Java code from the web pages, making the web site more maintainable over its lifespan and providing a viable alternative to Java Server Pages (JSPs) or PHP.
Velocity’s capabilities reach well beyond the realm of the web; for example, it can be used to generate SQL, PostScript and XML from templates. It can be used either as a standalone utility for generating source code and reports, or as an integrated component of other systems. For instance, Velocity provides template services forvarious web frameworks, enabling them with a view engine facilitating development of web applications according to a true MVC model.
sumber: https://velocity.apache.org
Mari kita mulai pembelajaran Apache Velocity Hello World
Berikut langkah-langkah untuk memulai Apache Velocity Hello World:
1). Pertama download library apache velocity disini
2). Buka editor eclipse kita, buat project java dengan nama “Belajar Apache Velocity”
3). Create folder “lib”, lalu copy jar Apache Velocity ke folder “lib”, kemudian jar Apache Velocity add to “Build Path”
4). Create folder “resources”, aktifkan folder “resources” sebagai “Source Folder” >> build path >> Use as Source Folder
5). Create file “helloworld.vm” simpan di folder “resources”
6). Buka file “helloworld.vm” lalu ketikkan coding dibawah ini:
1 |
Hello $name! Welcome to Apache Velocity! |
7). Buat Class dengan nama “HelloWorld” jangan lupa saat create class centang “static void main”
8). Tambahkan coding pada method main, seperti dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) throws Exception { VelocityEngine ve = new VelocityEngine(); ve.init(); Template template = ve.getTemplate("./resources/helloworld.vm"); StringWriter sw = new StringWriter(); VelocityContext context = new VelocityContext(); context.put("name", "Yulianto Wijaksana"); template.merge(context, sw); System.out.println(sw.toString()); } |
9). Run & Output
Hello Yulianto Wijaksana! Welcome to Apache Velocity!
Semoga Membantu,
Salam berbagi,
Yulianto Wijaksana