1. 什么是velocity
Velocity[vəˈlɑ:səti],名称字面翻译为:速度、速率、迅速。该项目的开源地址:http://velocity.apache.org/,它是一个基于Java的模板引擎,什么叫基于Java的模板引擎,就是说,在velocity中可以直接引用Java定义的对象。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。
2. Velocity是怎么被解析的
我们知道使用jsp技术,让Java代码出现在view层,不易于维护,增加了前端代码量。而velocity则正是jsp技术的替代者。velocity是一种template引擎,利用先编辑完的格式来作为大纲,把一些需要变化的地方作为参数传入,显示时将模板和参数合并,达到最终输出的样子。
3. Velocity渲染过程
- 首先初始化启动Velocity引擎,可以通过 Velocity.init()或者新建VelocityEngine类,并调用其中的init()方法;
- 创建一个 VelocityContext对象,将变量名与值关联起来,与HashMap对象相类似。可以直接将值传递给页面进行引用;
- 获取一个模板,利用Velocity.getTemplate()获取一个渲染模板,即要将数据最终渲染在哪个页面上去。
- 创建一个输出流,将上述创建的数据最终渲染到模板上,采用的方法template.merge()。
1 try { 2 Velocity.init("velocity.properties"); 3 VelocityContext context = new VelocityContext(); 4 String templateFile = "template.vm"; 5 context.put("paramObject", "onlyone"); 6 Template template = null; 7 template = Velocity.getTemplate(templateFile); 8 BufferedWriter writer = new BufferedWriter(new FileWriter("velocity.data")); 9 if (template != null) template.merge(context, writer); 10 writer.flush(); 11 writer.close(); 12 } catch (Exception e) { 13 e.printStackTrace(); 14 }
4. Velocity与SpringMVC的整合
1 <dependency> 2 <groupId>org.apache.velocity</groupId> 3 <artifactId>velocity</artifactId> 4 <version>1.7</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.apache.velocity</groupId> 9 <artifactId>velocity-tools</artifactId> 10 <version>2.0</version> 11 </dependency>
首先,需要在Maven项目中的pom.xml中引入velocity-1.7.jar包和velocity-tools-generic-2.0.jar包,为SpringMVC配置多视图,并添加velocity的视图配置(由于Velocity是用来连接Model和View层的),既然,Velocity是要依赖于SpringMVC的,那么我们先看看在Web.xml中对于SpringMVC的相关配置,
1 <!--Spring 服务层的配置文件 --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>classpath:applicationContext.xml</param-value> 5 </context-param> 6 7 <!--Spring 容器启动监听器 --> 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10 </listener> 11 12 <!-- Spring MVC 的Servlet,它将加载WEB-INF/springDispatcher-servlet.xml 的配置文件,以启动Spring MVC模块--> 13 <servlet> 14 <servlet-name>springDispatcher</servlet-name> 15 <servlet-class> 16 org.springframework.web.servlet.DispatcherServlet 17 </servlet-class> 18 <init-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value> 21 /WEB-INF/springDispatcher-servlet.xml 22 </param-value> 23 </init-param> 24 </servlet> 25 26 <servlet-mapping> 27 <servlet-name>springDispatcher</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping>
Spring MVC是基于DispatcherServlet来拦截请求,并找到相应的控制器进行业务逻辑处理。从web.xml中,我们可以看到web容器在启动的时候,它会加载
WEB-INF/springDispatcher-servlet.xml的配置文件,来启动MVC模块,所以我们的Velocity相关信息要配置在springDispatcher-servlet.xml中
1 <--1、对模型视图名称的解析,即在模型视图名称添加前后缀 --> 2 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 3 <property name="allowSessionOverride" value="true"/> 4 <property name="exposeSessionAttributes" value="true"/> 5 <property name="cache" value="true"/> 6 <property name=”prefix” value=”/WEB-INF/templates/”/> 7 <property name="suffix" value=".vm"/> 8 <property name="contentType"> 9 <value>text/html;charset=UTF-8</value> 10 </property> 11 </bean> 12 13 <--2、velocity的一些设置 --> 14 <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 15 <property name="resourceLoaderPath"> 16 <value>velocity/</value> 17 </property> 18 <property name="velocityProperties"> 19 <props> 20 <prop key="input.encoding">UTF-8</prop> 21 <prop key="output.encoding">UTF-8</prop> 22 <prop key="contentType">text/html;charset=UTF-8</prop> 23 </props> 24 </property> 25 </bean>
这部分配置文件中,主要配置两个部分:velocityViewResolver( 配置Velocity视图解析器)和velocityConfigurer(配置Velocity引擎)在配置的第一部分,我们定义了模型视图名称的解析规则,即使用了velocity模板视图解析器:VelocityViewResolver,因为在SpringMVC中有视图解析器viewResolver,通过这段配置文件,我们可以寻找采用模板的视图配置。于此同时,还定义了*.vm模板在后端的存放路径,这样Spring就与velocity模板技术整合起来了。在配置的第二部分,则定义了velocity的一些属性配置,包括定义前端显示页面的存放路径和页面的编码格式等。
此外,我们参考一下SpringMVC中解析jsp的视图解析器的配置,参考配置如下:
1 <!-- SpringMVC默认的视图解析器ViewResolver,负责解析jsp文件 --> 2 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 3 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 4 <property name="prefix" value="/WEB-INF/" /> 5 <property name="suffix" value=".jsp" /> 6 </bean>
5. Velocity与WebX的整合
6. Velocity源码分析及性能优化
http://agapple.iteye.com/blog/1051724
7. Velocity常用语法回顾
http://www.cnblogs.com/yjmyzz/p/4146699.html