版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yyyCHyzzzz/article/details/56679732
maven依赖
<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>
注意一点: 一定还需要引入这个包,要不然会疯狂报错
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.3.RELEASE</version> </dependency> mvc配置文件
<!--jsp视图解析器-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="order" value="1"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- velocity环境配置 -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<!-- velocity配置文件路径 或者直接用velocityProperties属性 -->
<property name="configLocation" value="classpath:velocity.properties"/>
<!-- velocity模板路径 -->
<property name="resourceLoaderPath" value="/WEB-INF/templates/"/>
</bean>
<!-- velocity视图解析器 -->
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="order" value="0"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
<property name="cache" value="true"/>
<property name="suffix" value=".vm"/>
<property name="layoutUrl" value="layout/layout.vm"/>
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
<property name="exposeSessionAttributes" value="true" /><!--是否开放request属性-->
<property name="requestContextAttribute" value="request"/><!--request属性引用名称-->
<property name="dateToolAttribute" value="dateTool"/>
<property name="numberToolAttribute" value="numberTool"/>
</bean>
velocity.properties
#设置字符集
#encoding
input.encoding =UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval =1
velocimacro.library.autoreload=false
项目目录结构
layout.vm
<html>
<head>
<title>$!page_title</title>
#parse("default/header.vm")
</head>
<body>
<div>
$screen_content
</div>
</body>
</html>
header.vm
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="3600"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
test.vm
<html>
<head>
<title>Spring MVC and Velocity</title>
</head>
<body>
<h1>Spring MVC and Velocity</h1>
${hello}
<hr />
Copyright © 2014 lm
</body>
</html>
TestController
/**
* Created by Stay on 2017/2/23 17:43.
*/
@Controller
@RequestMapping(value = "/test")
public class TestController {
@RequestMapping(value = "/velocity", method = RequestMethod.GET)
public String getTest(Model model) {
model.addAttribute("hello", "test velocity");
return "test";
}
}