---
title: "velocity在spring中的使用"
author: "chenpingtai2008"
date: "2010-08-31T13:31:42Z"
tags: ["Apache", "Spring", "Web", "XML", "velocity"]
categories: ["非技术"]
slug: "velocity-spring"
draft: false
lastmod: "2010-08-31T13:31:42Z"
id: 751650
url: "https://chenpingtai2008.iteye.com/blog/751650"
---

velocity在spring中的使用 关键字: spring velocity配置 之前做搜索时候，前台展示使用了velocity,由于时间比较急，所以也没深入使用，这里主要记一下配置及简单使用 1 web.xml文件里的配置： Xml代码 velocity org.apache.velocity.tools.view.servlet.VelocityViewServlet org.apache.velocity.toolbox classpath:toolbox.xml org.apache.velocity.properties classpath:velocity.properties 10 velocity \*.vm velocity org.apache.velocity.tools.view.servlet.VelocityViewServlet org.apache.velocity.toolbox classpath:toolbox.xml org.apache.velocity.properties classpath:velocity.properties 10 velocity \*.vm toolbox.xml，velocity有自己决定放到什么位置，这里指定路径就可以 velocity.properties里velocimacro.library = VM\_global\_library.vm，所指文件在下面velocityConfigurer指定的resourceLoaderPath下 当然要加入需要的jar包， velocity-1.4.jar velocity-dep-1.4.jar velocity-dvsl-1.0.jar velocity-tools-1.4.jar velocity-tools-generic-1.4.jar velocity-tools-view-1.4.jar 有这些就全了，呵呵 2 配置viewResolver 首先必须要先配置velocityConfigurer Xml代码 classpath:velocity.properties WEB-INF/velocity/ loopCoounter 0 UTF-8 UTF-8 text/html;charset=UTF-8 classpath:velocity.properties WEB-INF/velocity/ loopCoounter 0 UTF-8 UTF-8 text/html;charset=UTF-8 resourceLoaderPath指定页面存放的文件夹，会自动到这个文件夹下找文件 第二处主要制定编码，防止页面出现乱码的情况，虽然在src的velocity.properties里也指定了编码但还是出现乱码的情况，这边重新指定。 Xml代码 .vm true true text/html;charset=UTF-8 .vm true true text/html;charset=UTF-8 这样基本就可以用了，,vm文件放在web-inf/velocity下 results会找到web-inf/velocity下的results.vm文件 4 还没深入研究 不知道为什么toolbox不能用，总是报 java.lang.NullPointerException at org.springframework.web.servlet.view.velocity.VelocityToolboxView.createVelocityContext(VelocityToolboxView.java:114)错误，toolbox.xml放到web-inf/velocity下也不行，有知道的朋友说一下，网上搜了半天，只有两个韩国网站，又看不懂韩语，呵呵