velocity请到http://velocity.apache.org/download.cgi下载。
添加Velocity:
项目完成时,结构如下:![](http://static.oschina.net/uploads/space/2015/0114/115854_Wusu_940565.png)
然后,菜单栏“File”-> “Project properties (HelloSpring)”->“Libraries”,把velocity的jar以及其依赖的一些jar全部引入:
![](http://static.oschina.net/uploads/space/2015/0114/120132_G5Nn_940565.png)
结果:
![](http://static.oschina.net/uploads/space/2015/0114/120228_cVeB_940565.png)
下面看一下各个文件的源码。
/web/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
/web/WEB-INF/dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="letian.spring.controller" />
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/template/</value>
</property>
<property name="configLocation" value="/WEB-INF/template/velocity.properties"/> <!--在配置中防止中文乱码-->
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix">
<value>.vm</value>
</property>
<property name="contentType">
<value>text/html;charset=UTF-8</value> <!--防止中文乱码-->
</property>
</bean>
</beans>
/web/WEB-INF/template/velocity.properties:
input.encoding=UTF-8
output.encoding=UTF-8
/web/WEB-INF/template/index.vm:
<html>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<head>
<title>My first example using Spring 3 MVC</title>
</head>
<body>
<h2>你好</h2>
</body>
</html>
/web/WEB-INF/template/hello.vm:
<html>
<head>
<title>Velocity</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
/src/java/letian/spring/controller/HelloController.java:
package letian.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping(value = "/",method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hi, I am Velocity");
return "hello";
}
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public ModelAndView printHi(ModelMap model) {
return new ModelAndView("index");
}
}
打开浏览器,访问 http://localhost:8080/HelloSpring/,可以看到:
![](http://static.oschina.net/uploads/space/2015/0114/121137_pZak_940565.png)
访问 http://localhost:8080/HelloSpring/hi,可以看到:
![](http://static.oschina.net/uploads/space/2015/0114/121158_EbdF_940565.png)
更多+参考
2、Spring MVC整合Velocity
http://blog.csdn.net/qust008/article/details/9623513
3、spring mvc sitemesh velocity整合
http://blog.csdn.net/qust008/article/details/9625179
sitemesh为模板引擎提供了模板继承的功能。
velocity下载
http://velocity.apache.org/download.cgi
Velocity:Part 1 - Basic Application and Environment Setup
http://wiki.apache.org/velocity/VelocityAndSpringStepByStep
Spring下velocity页面中文乱码问题
http://jackandroid.iteye.com/blog/579655
Java开源模板引擎:给出了很多的基于Java的模板引擎。
http://www.open-open.com/21.htm
FreeMarker:另一个模板引擎
http://freemarker.org/