点我吧工作总结(技术篇) Velocity

时间:2023-03-09 13:02:26
点我吧工作总结(技术篇) Velocity

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渲染过程

  1. 首先初始化启动Velocity引擎,可以通过 Velocity.init()或者新建VelocityEngine类,并调用其中的init()方法;
  2. 创建一个 VelocityContext对象,将变量名与值关联起来,与HashMap对象相类似。可以直接将值传递给页面进行引用;
  3. 获取一个模板,利用Velocity.getTemplate()获取一个渲染模板,即要将数据最终渲染在哪个页面上去。
  4. 创建一个输出流,将上述创建的数据最终渲染到模板上,采用的方法template.merge()。
 try {
Velocity.init("velocity.properties");
VelocityContext context = new VelocityContext();
String templateFile = "template.vm";
context.put("paramObject", "onlyone");
Template template = null;
template = Velocity.getTemplate(templateFile);
BufferedWriter writer = new BufferedWriter(new FileWriter("velocity.data"));
if (template != null) template.merge(context, writer);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}

代码示例

4. Velocity与SpringMVC的整合

 <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>

pom.xml中引入velocity-1.7.jar包

  首先,需要在Maven项目中的pom.xml中引入velocity-1.7.jar包和velocity-tools-generic-2.0.jar包,为SpringMVC配置多视图,并添加velocity的视图配置(由于Velocity是用来连接Model和View层的),既然,Velocity是要依赖于SpringMVC的,那么我们先看看在Web.xml中对于SpringMVC的相关配置,

 <!--Spring 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!--Spring 容器启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring MVC 的Servlet,它将加载WEB-INF/springDispatcher-servlet.xml 的配置文件,以启动Spring MVC模块-->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springDispatcher-servlet.xml
</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

web.xml中配置SpringMVC

Spring MVC是基于DispatcherServlet来拦截请求,并找到相应的控制器进行业务逻辑处理。从web.xml中,我们可以看到web容器在启动的时候,它会加载

WEB-INF/springDispatcher-servlet.xml的配置文件,来启动MVC模块,所以我们的Velocity相关信息要配置在springDispatcher-servlet.xml中
 <--1、对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="allowSessionOverride" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="cache" value="true"/>
<property name=”prefix” value=”/WEB-INF/templates/”/>
<property name="suffix" value=".vm"/>
<property name="contentType">
<value>text/html;charset=UTF-8</value>
</property>
</bean> <--2、velocity的一些设置 -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>velocity/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
</props>
</property>
</bean>

springDispatcher-servlet.xml中的部分配置信息截取

这部分配置文件中,主要配置两个部分:velocityViewResolver( 配置Velocity视图解析器)和velocityConfigurer(配置Velocity引擎)在配置的第一部分,我们定义了模型视图名称的解析规则,即使用了velocity模板视图解析器:VelocityViewResolver,因为在SpringMVC中有视图解析器viewResolver,通过这段配置文件,我们可以寻找采用模板的视图配置。于此同时,还定义了*.vm模板在后端的存放路径,这样Spring就与velocity模板技术整合起来了。在配置的第二部分,则定义了velocity的一些属性配置,包括定义前端显示页面的存放路径和页面的编码格式等。

此外,我们参考一下SpringMVC中解析jsp的视图解析器的配置,参考配置如下:

 <!-- SpringMVC默认的视图解析器ViewResolver,负责解析jsp文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>

SpringMVC默认的视图解析器ViewResolver

5. Velocity与WebX的整合

6. Velocity源码分析及性能优化

  http://agapple.iteye.com/blog/1051724

7. Velocity常用语法回顾

  http://www.cnblogs.com/yjmyzz/p/4146699.html