spring mvc集成velocity配置

背景:最近遇到一个基于spring mvc的web项目,需要使用velocity模板引擎,首先需要学习如何集成velocity模板引擎

开发环境:

  • 操作系统:mac os x
  • JDK版本:1.8.0_45
  • Spring版本: 4.0.6
  • IDE:intellj idea

添加配置:

需要在spring配置文件中添加velocity支持:

       <!-- 配置velocity引擎 -->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
              <property name="contentType" value="text/html;charset=UTF-8" />
              <property name="suffix" value=".vm" />
              <property name="layoutUrl" value="empty_layout.vm" />
       </bean>
       <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
              <property name="resourceLoaderPath">
                     <value>views/</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>

配置说明:

  • viewResolver:

spring定义的viewResolver接口,视图解析器,org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver是它的velocity实现类,具体可以参考spring的相关技术文章,如果不使用velocity的布局功能,这里可以改为:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  • velocityConfigurer:

resourceLoaderPath定义了vm文件的存储目录
velocityProperties定义了velocity的常用配置,也可以通过properties文件去定义,然后通过configLocation属性引入

<property name="configLocation" value="classpath:velocity.properties" /> 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容