使用Velocity开发web程序

         Velocity通过反射机制使得能够在vm里面就像在JAVA程序里一样操作相应的对象。Velocity语法简单,相信一般人有几分钟就能编写velocity模板了,利用toolbox.xml使开发人员能够定义一些工具类在vm里面调用,velocity.properties文件能够定义一些参数设置(像velocityCount:全局变量的初始值)。当然velocity也存在一些不足。例如:一个结果集对象列表,对象里面存在一个double类型的变量,想通过循环的时候将此值进行累加,这样我们就要定义一个有初始值的变量$sumAmount=0,velocity的运算功能很弱,如果用此变量进行累加是不行的,velocity不能自动把int型的向上转型至double,我在做的时候是通过javascript实现的(这样就比较复杂了)。

velocity的一些语法请见:http://www-128.ibm.com/developerworks/cn/java/j-velocity/

在此主要介绍一下velocity的开发配置(按正常创建web工程,修改web工程中的如下部分):

1.需要的jar包:

    velocity-1.4.jar 下载地址:http://apache.justdn.org/jakarta/velocity/velocity-1.4/velocity-1.4.zip
    velocity-tools-1.2.jar 下载地址:http://apache.justdn.org/jakarta/velocity-tools/binaries/velocity-tools-1.2.zip
2.web.xml

<servlet-name>velocity</servlet-name>

          <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet </servlet-class>

          <init-param>

                   <param-name>org.apache.velocity.toolbox</param-name>

                   <param-value>/WEB-INF/toolbox.xml</param-value>

         </init-param>

         <init-param>

                <param-name>org.apache.velocity.properties</param-name>

                <param-value>/WEB-INF/velocity.properties</param-value>

         </init-param>

        <load-on-startup>10</load-on-startup>

<servlet-mapping>

         <servlet-name>velocity</servlet-name> 

         <url-pattern>*.vm</url-pattern>

</servlet-mapping>

其中toolbox.xml(不是必须的)里面定义了一些开发中需要的工具类,Velocity.properties(不是必须的) 文件中定义了一些velocity的初始值。根据个人情况确定两个文件是否添加。如果需要一般就都放在WEB-INF下面就可以了。

这样就可以编写自己的vm文件进行正常使用了。

3.eclipse中开发velocity的插件veloeclipse:

 Velocity插件在线更新地址及方式:

Veloeclipse is a HTML/Velocity Editor for Eclipse, it is based on veloedit for velocity parsing and outline but adds all the html features you'd expect to find in a html editor

 

Features:

  • User defined Templates
  • Smart indentation of velocity directives ( on pressing return or when using tabs)
  • Smart indentation of html tags( on pressing return or when using tabs)
  • Syntax highlighting for both html and velocity
  • Autocompletion and Content assist for tags,directives and references (Ctrl + space in html attribute values or when opening or closing tags)
  • Preview html code
  • jumping to statring tag/directive when hovering on the closing tag/directive and then pressing Ctrl+ mouseclick
  • opening html definitions on tags when pressing Ctrl+ mouseclick

Installing:

Help -> Software Updates -> Find and Install... -> Search for new features to install -> Add Update Site... ->

Name : Veloeclipse

URL: http://propsorter.sourceforge.net/veloeclipse

 

 

Akmal Sarhan

Please note that it is only compatible with Eclipse 3.0 M8 and higher

按照上面说明在eclipse中在线更新就可以了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值