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
- Velocity 的简单配置及使用
- velocity的使用-配置(Maven)
- 使用velocity生成静态页面,及velocity的循环控制
- velocity模板的使用简单的语法
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- Velocity简单使用
- velocity使用配置
- Velocity的简单介绍
- velocity的简单学习
- svn简单的配置及使用
- logback的简单配置及使用
- SVN的配置及简单使用
- saiku的简单配置及使用
- OKHttp的配置及简单使用
- Mybatis的配置及简单使用
- velocity的使用 转载
- 我对Android的认识
- collection View学习笔记
- 后退N帧协议总结
- git无法push
- Internet Control Message Protocol (ICMP) Parameters
- Velocity 的简单配置及使用
- Swift中与Objective-C的区别 -> 值类型和引用类型
- google chrome 官方下载安装的方法和离线包官方下载
- sql中的约束条件
- Android 动画应用大集合
- Java-ThreadPool 线程池
- 简易解说拉格朗日对偶(Lagrange duality)
- Java程序执行Linux命令
- bzoj3265: 志愿者招募加强版 单纯形