spring3mvc集成velocity

一、导入velocity库
访问Velocity位于http://jakarta.apache.org/velocity的主页,到官网下载velocity库,解压后将里面的velocity-1.7.jar拷贝到sping项目工程里面的web库里,另外,因为velocity还用到两个依赖库commons-collections-3.2.1.jar和commons-lang-2.4.jar,这两个在velocity下载包的lib里面有,将这两个也放在web项目环境即WEB-INF/lib/下; html

二、 配置Velocity引擎
首先须要配置的是Velocity引擎本身。要作到这点,能够经过如下方式在Spring配置文件中声明一个VelocityConfigurer Bean: java

<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">

    <property name="resourceLoaderPath">

      <value>WEB-INF/velocity/</value>

    </property>

    </bean>

三、解析Velocity视图
要使用Velocity模板视图,你必须作的最后一件事情是配置一个视图解析器。具体地说,须要以以下方式在Spring上下文配置中声明一个VelocityViewResolver Bean:
web

<bean id="velocityViewResolver" class="org.springframework.

          web.servlet.view.velocity.VelocityViewResolver">

    <property name="suffix"><value>.html</value></property>

</bean>


 四、因为本项目使用了多视图解析器,根据视图名后缀选择,因此控制器返回的视图名称带指向velocity解析器的后缀,代码以下: spring

@RequestMapping(value="/indexvm")
	public String index_vm(Model model){
		model.addAttribute("userName", "kkk");
		return "index_vm";
	}

相应的视图模板index.html页面以下: apache

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is index html page!
welcome $!{userName}
</body>
</html>
相关文章
相关标签/搜索