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?相关推荐
- mybatis-plus 错误java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
https://blog.csdn.net/qq_39609151/article/details/82855305 mybatis-plus 错误java.lang.NoClassDefFoundE ...
- 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 - ======================= ...
- mybatis-plus自动生成的时候报错java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
当使用mybatisplus的代码自动生成的时候报错 09:02:44.188 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerato ...
- 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 ...
- Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Contex
关于使用mybatis-plus代码生成器报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apa ...
- 解决Mybatis Plus代码自动生成时报错: Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context
解决Mybatis Plus代码自动生成时报错: Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Co ...
- Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
解决方法: <!-- 模板引擎 --> <dependency><groupId>org.apache.velocity</groupId><ar ...
- mybatisplus代码生成报错:Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/velocity
加上下面的依赖 <dependency><groupId>org.apache.velocity</groupId><artifactId>veloci ...
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
http://blog.csdn.net/lxpbs8851/article/details/11018933 起因是我重装了mysql数据库. 安装之后 把访问权限都配置好 : GRANT ALL ...
最新文章
- MPB:中科院城环所杨军组-​​水体浮游植物采集与鉴定
- flask创建mysql表_请教一下,flask-sqlalchemy 怎么动态创建数据表?
- 4 个最好的 Linux 引导程序
- 负载均衡下ajax第二次请求,会话清除第二个AJAX电话
- python 可视化_Python可视化|Matplotlib40LaTeX in Matplotlib和python
- 今天痛下决心,把开发人员的外网给断了,不断是不好管了,人心散了队伍就不好带...
- Windows 通过 putty 连接 虚拟机下linux 问题
- 用计算机计算汉坦,河北省Ⅱ型汉坦病毒流行特征及与细胞自噬相互作用研究
- centos安装桌面和远程连接
- Android 自定义View -- 简约的折线图
- Docker安装应用程序(Centos6.5_x64)
- vmware 里MAC 鼠标能移动 无法单击
- Java应用开发的一条重要经验:先建立基础设施
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第5节 使用骨架创建maven的java工程_13使用骨架创建maven的web工程...
- CocosBuilder 教程
- VSCode Latex Workshop 设置 XeLatex 编译
- FastStone Capture(FSCapture) 注册码
- 视频p2p php,m3u8视频播放器集成p2p加速源码分享
- visual C#(二十五)实现UWP应用的用户界面
- 除了Google,还有哪些公司能做出AlphaGo?