只需2分钟,简单构建velocity web项目

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象

  velocity的语法非常简单。这里不多介绍。

  我们平时的web项目,通常的开发流程是前端写好静态页面。后端将静态页面改成jsp,在相应的需要替换数据的地方,使用 jstl、EL表达式等接收action模块传递过来的数据。一般使用action来处处理参数,调用service层来处理业务逻辑,service层调用dao层从数据库取数据。我们可以看到,大体上就是后端发送数据给前端。

  这里一般会有一个很大的问题。如果业务变更,例如一个页面本来要显示一个表格,现在需要显示3个表格,另外的2个表格是从之前的其他页面合并的。mvc模式的好处就是,此时,除了页面的改动之外,做业务逻辑处理的service层和dao层,都不需要变化,只需要改动相应的action层,调用所需的service模块,将所需要的数据输出。

  而如果使用velocity,除了页面的改动之外,其他都不需要改动。

  velocity做web项目与我们平时的项目最大的不同是由前端从后端拿数据。需要什么就拿什么。我们完全可以通过velocity把前端和后端完全分离开来。

  下面使用一个简单的例子模拟一下velocity的这种思想。

  (由于velocity自身并不提供任何web相关的功能,它只是通过模板生成格式文档。但是,开发web应用的时候,需要有一个框架来处理HTTP请求。velocity有一个非常好的子项目velocity-tools,使用它的VelocityViewServlet可以非常方便的实现这个功能。如下)

  1、依赖包只有2个:最新的 velocity-1.6.2.jar 和 velocity-tools-2.0.jar,网址:http://velocity.apache.org/download.cgi

  2、首先看一下我们的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> <servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/conf/velocity-toolbox.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>

  3、然后看一下简单的页面:

<html>
<body> this is the word : $mytool.getHello() </body>
</html>

  4、接下来是最重要的velocity-toolbox.xml 类,这里的工具类就是在页面可以直接调用的类了。如下:

<?xml version="1.0" encoding="UTF-8"?>

<toolbox>

  <tool>
<key>mytool</key>
<scope>request</scope>
<class>com.chandler.tool.MyTool</class>
</tool> </toolbox>

  指定了请求到来时的工具类实例的生命周期为request

  5、OK,就这样,可以访问了,如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUIAAABzCAIAAABinpx3AAAUoklEQVR4nO2dW1AbV5rH9bT7kpqqrdp9yezWPuVx52GqssywNclMXJn4OjHGg+8YY1s2sU1sE2zHdozjkIRMLsvOzia1SRRMBOEiY4QEAoEQ4i5uksVFXIQAA8YgqaWWWhduCfvQoatpdbdaqBvU7Pcrlat1dPo7/3PO9+9zdIyxJAAAgMiRbLcAceP1+Wbn5q1jE+bBEaNpoLNvoLNvwGgaMA+OWMfss3PzPgzbbo3AzgdsvElciHto1N7R1z9sm1hwurw+LBAIBIPBYDAYCAS8Pmze4Rq2TXT09VvHJhC3Z7v1AjsZsHHU+HzY6PiU0TQwOzcfDAZDrASDwdm5eaNpYHR8CoOVGRAGiR+IBsTt7nkyZJt4imF+dgOTwTBszD5lGhj2oF6fz7fdnQB2GmDjKHC6kC7TwILDyd3AZBYczi7z4NffyAoKCh4CAH+AjbmCuN3GvgEX4g7GgBNxP7HalpZX1gCAP3aUjQm38B7Z6/N1Pxl8Nr8Qi4dxXG50cHTixx9/2u6pB3YOO83GnX39Qth4xDY5Zp+K3cPBYHBpaWlyZm7muWO7px7YOYCNI4O43Z29/T4fp3PmIevw8Mgoe53Q4mL/sB221gBfSLAo8Xq9d3Jy7+Tker3eaO+NCsvAYMrJs1HdEggEOnotgUCAXyXWMfvUzDMuHjZ29RSXPioqUZjMFjYbh0IOxPN0dp5pVoLB4NDQkF6vV6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uMWIfoFvzYAAEQP2BgARA/YGABED9gYAEQP2BgARI/kIQAAIkeyBgCAyAEbA4DoARsDgOgBGwOA6AEbA4DoARsDgOgBGwOA6AEbA4DoARsDgOgBGwOA6AEbA4DoARsDgOiJ2sb4L3DZ3Kd8tRKfkXmBd3nE79yhfcvllojlPOoUNKyg+reXzXRJ1DZeY9UfD/AujxKQY3ymaqKbl811X1xE7hLtU1kYMYIgOv1gY0HDxvnsbw6wcdzB3WabMyTYWIhWthdO35EoGYNf05Yz5RZTKPZ2+YrDUT/lFi7BmaKFxyG/jbFpigAuNZnesjTKFDzivHCRtMaQP7TltPHZB5NFEkf9Eecrrtjkahw++uz12Stwb5evOLT6o4offheXOJREYarD0u4mkIQRUSeLDF7mhWn8o5oXljzkKz9Zhiuu4GFTHe0wcVW2VXHCH8Bcnr6btnFEhULYmPYte3+FszHHyOFvOT6GNpGfTOMQz9YlsxU2XtvspkugOBynn0vY8D9Z4jM1sY025n4XS/kW2Jj2mkcbRys13tgiG0dVQeg4YGMuDcWPjfkqZ2odbBz5LZcKvLQbe5xY0pHFq1w2aWQN3Pu1uRHYdLozlW9iXnhZXbmMc/goRaV/59h4beNYSNYJfyvZCOX28HL25mKPE61+ShNcwtJe08aJKJ4yjFF1KmK1qPq7ifHfxLxwFEbbdHhzTGrDLzjqZ6kch8S7PgAAIgI2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0QM2BgDRAzYGANEDNgYA0fN/5p+llbcGJh8AAAAASUVORK5CYII=" alt="" />

  可以看到,velocity的这种特性,使得它开发小型的网站变得非常便捷!很大的提高了开发效率!

  如果文中有什么不对的地方,欢迎指正!谢谢!

  转载请注明出处:http://www.cnblogs.com/zrtqsk/p/4010840.html 谢谢!

上一篇:APP UI设计趋势:为好设计而动


下一篇:服务器webapi集成极光推送学习笔记