日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java velocity是什么意思,什么是Apache Velocity?

發布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java velocity是什么意思,什么是Apache Velocity? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Can someone please explain, what is Apache Velocity ?

what is its purpose ?

It would be nice to provide an example along with it.

Thanks in advance.

解決方案

Apache Velocity is a template engine. That means that you can add variables to a context, load a template in which those variables are referenced and render a text from this template where the references to the variables are replaced with the variable's actual value.

It's purpose is to separate design and static content from code. Take a website for example. You don't want to create HTML inside your java code, do you? You would have to recompile your app every time you change a bit of design and you would polute your code with unnecessary design clutter. You would rather want to get your variables, either computed or from a database or whatever and have a designer create a HTML template in which your variables are used.

Some pseudo code to make it clear:

/* The user's name is "Foo" and he is of type "admin"*/

User user = getUserFromDatabase("Foo");

/* You would not add hard coded content in real world.

* it is just to show how template engines work */

String message = "Hello,";

Velocity.init(); /* Initialises the Velocity engine */

VelocityContext ctx = new VelocityContext();

/* the user object will be available under the name "user" in the template*/

ctx.put("user",user);

/* message as "welcome" */

ctx.put("welcome",message);

StringWriter writer = new StringWriter();

Velocity.mergeTemplate("myTemplate.vm", ctx, writer);

System.out.println(writer);

Now given a file called myTemplate.vm

${welcome} ${user.name}!

You are an ${user.type}.

The output would be:

Hello, Foo!

You are an admin.

Now let's assume the flat text should be HTML instead. The designer would change myTemplate.vm to

${welcome} ${user.name}

You are an ${user.type}

So the output would be a html page without a single change in the java code.

So the use of a template engines like Velocity (there are others, e.g. Thymeleaf or Freemarker) let designers do a designer's job and programmers do a programmer's job with minimal interference to each other.

總結

以上是生活随笔為你收集整理的java velocity是什么意思,什么是Apache Velocity?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。