ITeye博客

Spring MVC整合Velocity

wusuoya · 2015-01-27 11:59 · springMVC · 开源软件 · Spring · velocity · MVC · Markdown

引用链接: http://blog.csdn.net/qust008/article/details/9623513

Spring MVC整合Velocity,就要引入相关的包,需要导入的包如下图

pom引入:

        <dependency>

            <groupId>org.apache.velocity</groupId>

            <artifactId>velocity</artifactId>

            <version>1.6.2</version>

        </dependency>

        <dependency>

            <groupId>org.apache.velocity</groupId>

            <artifactId>velocity-tools</artifactId>

            <version>2.0</version>

        </dependency>

 

        <dependency>

 

然后,就是进行很简单的配置了,那在这里,就修改下spring-mvc.xml内容,内容修改如下

 

[html] view plaincopy

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--看到下面的beans这个元素标签没有,必须有标签的声明 -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  13. <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
  14. <context:component-scan base-package="qust.thb.*" />
  15. <!-- 支持spring3.0新的mvc注解 -->
  16. <mvc:annotation-driven />
  17. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  18. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  19. <!-- ViewResolver -->
  20. <!--
  21. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  23. <property name="prefix" value="/WEB-INF/" />
  24. <property name="suffix" value=".jsp" />
  25. </bean>
  26. -->
  27. <!-- 配置Velocity视图解析器 -->
  28. <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  29. <property name="cache" value="false"/>
  30. <property name="order" value="1"/>
  31. <property name="suffix" value=".vm"/>
  32. <property name="contentType" value="text/html;charset=UTF-8"/>
  33. <property name="toolboxConfigLocation" value="WEB-INF/velocity/toolbox.xml"/>
  34. </bean>
  35. <!-- 配置Velocity引擎 -->
  36. <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  37. <property name="resourceLoaderPath" value="/WEB-INF/velocity/vm/"/>
  38. <property name="configLocation" value="classpath:velocity.properties"/>
  39. </bean>
  40. </beans>

然后在WEB-INF文件夹下创建velocity.properties文件,里面内容很简单,就两句话

 

input.encoding=UTF-8
output.encoding=UTF-8

然后,将view文件夹下新建一个文件,名为hello.vm,里面内容也很简单,如下

 

[html] view plaincopy

 

  1. <div>
  2. ${message}
  3. </div>

整体的目录结构如下图所示

 

 

输入地址http://localhost:8080/SpringMVC/user/getUser.do,访问正常访问

用到velocity,肯定要用到SiteMesh进行修饰,使得我们在由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条、一致的banner、一致的版权等。

下一篇介绍Spring MVC利用SiteMesh修饰velocity

http://blog.csdn.net/qust008/article/details/9625179

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。