Velocity 的简单配置及使用

之前一直使用 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值