Velocity 的简单配置及使用

来源:互联网 发布:淘宝红人模特排行榜 编辑:程序博客网 时间:2024/04/28 22:39

之前一直使用 JSTL 写前端的代码,if else 这种简单功能都没有真是丧心病狂。不管怎样,来到 velocity 的地盘,性能也是比JSP要强一些的,上手比较容易。

一、导包

<!-- velocity --><dependency>    <groupId>org.apache.velocity</groupId>    <artifactId>velocity</artifactId>    <version>1.7</version></dependency><dependency>    <groupId>org.apache.velocity</groupId>    <artifactId>velocity-tools</artifactId>    <version>2.0</version></dependency>

这里我使用的是maven,直接在 pom.xml 文件里加上这一段就好了。非maven用户请自行下载对应包。

二、注册web.xml文件

<!-- 定义Velocity --><servlet>    <servlet-name>velocitylayout</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></servlet><servlet-mapping>    <servlet-name>velocitylayout</servlet-name>    <url-pattern>*.vm</url-pattern></servlet-mapping>

需要在web.xml文件中注册velocity,init-param里面指定的是velocity-tools 和properties 文件的位置。其实properties 文件位置不写也是可以的,程序会自动定位到一个默认的文件中:velocity->org.apache.velocity.runtime.defaults 里面

如果你使用了struts,那你就需要在struts.xml加上下面这一段

<!-- velocity toolbox --><constant name="struts.velocity.toolboxlocation" value="/WEB-INF/toolbox.xml"/><constant name="struts.velocity.configfile" value="/WEB-INF/velocity.properties"/>

到了这一步,就已经能在vm文件中使用velocity了
test.vm

<html><head>    <title>Velocity</title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>    #set( $test = "test")    $test</body></html>

页面输出的结果就是

test

三、toolbox.xml文件

<?xml version="1.0" encoding="UTF-8"?><toolbox>    <tool>        <key>date</key>        <scope>request</scope>        <class>org.apache.velocity.tools.generic.DateTool</class>        <parameter name="format" value="yyyy-MM-dd HH:mm:ss"/>    </tool>    <tool>        <key>link</key>        <scope>request</scope>        <class>org.apache.velocity.tools.view.tools.LinkTool</class>    </tool>    <tool>        <key>messages</key>        <scope>request</scope>        <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>    </tool>    <tool>        <key>stringUtils</key>        <scope>request</scope>        <class>org.apache.velocity.util.StringUtils</class>    </tool>    <tool>        <key>math</key>        <scope>application</scope>        <class>org.apache.velocity.tools.generic.MathTool</class>    </tool>    <tool>        <key>esc</key>        <scope>request</scope>        <class>org.apache.velocity.tools.generic.EscapeTool</class>    </tool>    <tool>        <key>params</key>        <scope>request</scope>        <class>org.apache.velocity.tools.view.tools.ParameterParser</class>    </tool></toolbox>

配置完toolbox就能使用velocity更加强大的功能了
test.vm

<html><head>    <title>Velocity</title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>    $date.get("yyyy-MM-dd");</body></html>

页面结果是

2016-04-22
0 0