velocity在spring中的使用

之前做搜索时候,前台展示使用了velocity,由于时间比较急,所以也没深入使用,这里主要记一下配置及简单使用

 

1 web.xml文件里的配置:

<!-- Velocity 的配置 -->
	<servlet>
		<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>classpath:toolbox.xml</param-value>
		</init-param>
		<init-param>
			<param-name>org.apache.velocity.properties</param-name>
			<param-value>classpath:velocity.properties</param-value>
		</init-param>
		<load-on-startup>10</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>velocity</servlet-name>
		<url-pattern>*.vm</url-pattern>
	</servlet-mapping>

	<!-- Velocity 的配置结束 -->

 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

<bean id="velocityConfigurer"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="configLocation"><value>classpath:velocity.properties</value></property> 
		<property name="resourceLoaderPath">
			<value>WEB-INF/velocity/</value>
		</property>
		<property name="velocityProperties">
			<props>
				<prop key="directive.foreach.counter.name">
					loopCoounter
				</prop>
				<prop key="directive.foreach.counter.initial.value">
					0
				</prop>
				<prop key="input.encoding">UTF-8</prop>
				<prop key="output.encoding">UTF-8</prop>
				<prop key="contentType">text/html;charset=UTF-8</prop>
			</props>
		</property>
	</bean>

 resourceLoaderPath指定页面存放的文件夹,会自动到这个文件夹下找文件

 

第二处主要制定编码,防止页面出现乱码的情况,虽然在src的velocity.properties里也指定了编码但还是出现乱码的情况,这边重新指定。

 

<bean id="viewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<property name="suffix">
			<value>.vm</value>
		</property>
		<property name="exposeRequestAttributes">
			<value>true</value>
		</property>
		<property name="exposeSessionAttributes">
			<value>true</value>
		</property>
		<property name="contentType">
			<value>text/html;charset=UTF-8</value>
		</property>
<!--		<property name="toolboxConfigLocation" value="toolbox.xml"/>-->
<!--		<property name="toolboxConfigLocation">-->
<!--			<value>toolbox.xml</value>-->
<!--		</property>-->
	</bean>

 

 这样基本就可以用了,,vm文件放在web-inf/velocity下

<property name="errorView">
   <value>results</value>会找到web-inf/velocity下的results.vm文件
  </property>

 

4 还没深入研究

 

<!-- <property name="toolboxConfigLocation" value="toolbox.xml"/>-->
<!-- <property name="toolboxConfigLocation">-->
<!-- <value>toolbox.xml</value>-->
<!-- </property>-->

不知道为什么toolbox不能用,总是报

java.lang.NullPointerException
 at org.springframework.web.servlet.view.velocity.VelocityToolboxView.createVelocityContext(VelocityToolboxView.java:114)错误,toolbox.xml放到web-inf/velocity下也不行,有知道的朋友说一下,网上搜了半天,只有两个韩国网站,又看不懂韩语,呵呵

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值