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?相关推荐

  1. mybatis-plus 错误java.lang.NoClassDefFoundError: org/apache/velocity/context/Context

    https://blog.csdn.net/qq_39609151/article/details/82855305 mybatis-plus 错误java.lang.NoClassDefFoundE ...

  2. Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Context at c

    11:41:33.067 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ======================= ...

  3. mybatis-plus自动生成的时候报错java.lang.NoClassDefFoundError: org/apache/velocity/context/Context

    当使用mybatisplus的代码自动生成的时候报错 09:02:44.188 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerato ...

  4. mybatis-plus异常Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context

    在使用mybatis-plus进行代码生成时,出现报错: Connected to the target VM, address: '127.0.0.1:59589', transport: 'soc ...

  5. Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Contex

    关于使用mybatis-plus代码生成器报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apa ...

  6. 解决Mybatis Plus代码自动生成时报错: Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context

    解决Mybatis Plus代码自动生成时报错: Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Co ...

  7. Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Context

    解决方法: <!-- 模板引擎 --> <dependency><groupId>org.apache.velocity</groupId><ar ...

  8. mybatisplus代码生成报错:Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity

    加上下面的依赖 <dependency><groupId>org.apache.velocity</groupId><artifactId>veloci ...

  9. FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法

    http://blog.csdn.net/lxpbs8851/article/details/11018933 起因是我重装了mysql数据库. 安装之后 把访问权限都配置好 : GRANT ALL ...

最新文章

  1. MPB:中科院城环所杨军组-​​水体浮游植物采集与鉴定
  2. flask创建mysql表_请教一下,flask-sqlalchemy 怎么动态创建数据表?
  3. 4 个最好的 Linux 引导程序
  4. 负载均衡下ajax第二次请求,会话清除第二个AJAX电话
  5. python 可视化_Python可视化|Matplotlib40LaTeX in Matplotlib和python
  6. 今天痛下决心,把开发人员的外网给断了,不断是不好管了,人心散了队伍就不好带...
  7. Windows 通过 putty 连接 虚拟机下linux 问题
  8. 用计算机计算汉坦,河北省Ⅱ型汉坦病毒流行特征及与细胞自噬相互作用研究
  9. centos安装桌面和远程连接
  10. Android 自定义View -- 简约的折线图
  11. Docker安装应用程序(Centos6.5_x64)
  12. vmware 里MAC 鼠标能移动 无法单击
  13. Java应用开发的一条重要经验:先建立基础设施
  14. 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第5节 使用骨架创建maven的java工程_13使用骨架创建maven的web工程...
  15. CocosBuilder 教程
  16. VSCode Latex Workshop 设置 XeLatex 编译
  17. FastStone Capture(FSCapture) 注册码
  18. 视频p2p php,m3u8视频播放器集成p2p加速源码分享
  19. visual C#(二十五)实现UWP应用的用户界面
  20. 除了Google,还有哪些公司能做出AlphaGo?

热门文章

  1. optee运行时来了一个REE(linux)中断--代码导读
  2. Django生命周期与中间件
  3. 宝塔 mysql迁移_(2020年最新方法)如何快速迁移网站?使用宝塔一键迁移转移网站数据详细教程...
  4. linux xargs 前一条命令的输出作为下一条命令的参数
  5. go和python计算字节数组sha1
  6. 内核和用户模式下进程与线程创建
  7. 2020-11-19(栈帧)
  8. 005 Android之数据存储
  9. 【PHP】 php7.0 扩展列表
  10. 6、存储函数详解,创建,查看,修改,删除