Spring MVC整合Velocity

来源:互联网 发布:淘宝查gsx会泄露信息吗 编辑:程序博客网 时间:2024/05/16 11:56

velocity学习参考:http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/


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


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

[html] view plain copy
  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.   
  14.     <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->  
  15.     <context:component-scan base-package="qust.thb.*" />  
  16.     <!-- 支持spring3.0新的mvc注解 -->  
  17.     <mvc:annotation-driven />  
  18.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  19.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  20.   
  21.     <!-- ViewResolver -->  
  22.     <!--   
  23.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  24.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  25.         <property name="prefix" value="/WEB-INF/" />  
  26.         <property name="suffix" value=".jsp" />  
  27.     </bean>  
  28.      -->  
  29.        
  30.     <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
  31.         <property name="resourceLoaderPath" value="/WEB-INF/"/>  
  32.         <property name="configLocation" value="/WEB-INF/velocity.properties"/>  
  33.     </bean>  
  34.    
  35.     <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">  
  36.         <property name="cache" value="true"/>  
  37.         <property name="prefix" value=""/>  
  38.         <property name="suffix" value=".vm"/>  
  39.     </bean>  
  40.   
  41. </beans>  
然后在WEB-INF文件夹下创建velocity.properties文件,里面内容很简单,就两句话

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

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

[html] view plain copy
  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 0