如题,由于新项目需要,之前自己也没接触过Velocity,所以尝试整合时遇到几个问题,请大牛指点。
主要配置如下(无关属性已删减):
1、视图处理器:
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" />
<property name="prefix" value="screen/" />
<property name="stufix" value=".vm" />
<property name="exposeSpringMacroHelpers" value="true" /><!-- 默认支持,这里为测试,显式配置-->
</bean>
2、模板引擎:
<!-- Velocity引擎 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
<!-- <property name="configLocation" value="classpath:velocity.properties" /> -->
</property>
</bean>
3、属性文件velocity.properties:
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.cache = false
class.resource.loader.modificationCheckInterval = 2
#自定义宏库
velocimacro.library = velocity-macro.vm
velocimacro.library.autoreload = true
==========================================================================
主要日志显示如下:
2013-12-29 12:18:02,333 [DEBUG]----> Creating shared instance of singleton bean 'velocityConfigurer'
2013-12-29 12:18:02,333 [DEBUG]----> Creating instance of bean 'velocityConfigurer'
2013-12-29 12:18:02,333 [DEBUG]----> Eagerly caching bean 'velocityConfigurer' to allow for resolving potential circular references
2013-12-29 12:18:02,333 [DEBUG]----> Invoking afterPropertiesSet() on bean with name 'velocityConfigurer'
2013-12-29 12:18:02,364 [INFO]----> Loading Velocity config from [class path resource [velocity.properties]]
2013-12-29 12:18:02,364 [DEBUG]----> Resource loader path [/WEB-INF/velocity/] resolved to file [D:SpringMVC\WEB-INF\velocity]
2013-12-29 12:18:02,379 [INFO]----> ClasspathResourceLoader with name 'springMacro' added to configured VelocityEngine
2013-12-29 12:18:02,379 [INFO]----> **************************************************************
2013-12-29 12:18:02,395 [INFO]----> Starting Jakarta Velocity v1.4
2013-12-29 12:18:02,395 [INFO]----> RuntimeInstance initializing.
2013-12-29 12:18:02,395 [INFO]----> Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
2013-12-29 12:18:02,395 [INFO]----> Default ResourceManager initializing. (class org.apache.velocity.runtime.resource.ResourceManagerImpl)
2013-12-29 12:18:02,411 [INFO]----> Resource Loader Instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2013-12-29 12:18:02,411 [INFO]----> ClasspathResourceLoader : initialization starting.
2013-12-29 12:18:02,411 [INFO]----> ClasspathResourceLoader : initialization complete.
2013-12-29 12:18:02,411 [INFO]----> Resource Loader Instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2013-12-29 12:18:02,411 [INFO]----> ClasspathResourceLoader : initialization starting.
2013-12-29 12:18:02,411 [INFO]----> ClasspathResourceLoader : initialization complete.
2013-12-29 12:18:02,411 [INFO]----> ResourceCache : initialized. (class org.apache.velocity.runtime.resource.ResourceCacheImpl)
2013-12-29 12:18:02,426 [INFO]----> Default ResourceManager initialization complete.
2013-12-29 12:18:02,426 [INFO]----> Loaded System Directive: org.apache.velocity.runtime.directive.Literal
2013-12-29 12:18:02,426 [INFO]----> Loaded System Directive: org.apache.velocity.runtime.directive.Macro
2013-12-29 12:18:02,426 [INFO]----> Loaded System Directive: org.apache.velocity.runtime.directive.Parse
2013-12-29 12:18:02,426 [INFO]----> Loaded System Directive: org.apache.velocity.runtime.directive.Include
2013-12-29 12:18:02,426 [INFO]----> Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
2013-12-29 12:18:02,474 [INFO]----> Created: 20 parsers.
2013-12-29 12:18:02,474 [INFO]----> Velocimacro : initialization starting.
2013-12-29 12:18:02,474 [INFO]----> Velocimacro : adding VMs from VM library template : velocity-macro.vm
2013-12-29 12:18:02,490 [INFO]----> Velocimacro : added new VM : #test( code ) : source = velocity-macro.vm
2013-12-29 12:18:02,505 [INFO]----> ResourceManager : found config/velocity/velocity-macro.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : VM library template macro registration complete.
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : adding VMs from VM library template : org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : added new VM : #springMessage( code ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : added new VM : #springMessageText( code text ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : added new VM : #springTheme( code ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : added new VM : #springThemeText( code text ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,505 [INFO]----> Velocimacro : added new VM : #springUrl( relativeUrl ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springBind( path ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springBindEscaped( path htmlEscape ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormInput( path attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormPasswordInput( path attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormHiddenInput( path attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormTextarea( path attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormSingleSelect( path options attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormMultiSelect( path options attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormRadioButtons( path options separator attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,521 [INFO]----> Velocimacro : added new VM : #springFormCheckboxes( path options separator attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,536 [INFO]----> Velocimacro : added new VM : #springFormCheckbox( path attributes ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,536 [INFO]----> Velocimacro : added new VM : #springShowErrors( separator classOrStyle ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,536 [INFO]----> Velocimacro : added new VM : #springCloseTag( ) : source = org/springframework/web/servlet/view/velocity/spring.vm
2013-12-29 12:18:02,568 [INFO]----> ResourceManager : found org/springframework/web/servlet/view/velocity/spring.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : VM library template macro registration complete.
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : allowInline = true : VMs can be defined inline in templates
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : messages on : VM system will output logging messages
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : autoload on : VM system will automatically reload global library macros
2013-12-29 12:18:02,568 [INFO]----> Velocimacro : initialization complete.
2013-12-29 12:18:02,568 [INFO]----> Velocity successfully started.
2013-12-29 12:18:02,568 [DEBUG]----> Finished creating instance of bean 'velocityConfigurer'
=====================================================================================================
测试场景如下:
第一组:
1、不启用引擎中的自定义宏库(注释掉<property name="configLocation" value="classpath:velocity.properties" />,上面的日志就少了蓝色那段),其他配置不变,可以顺利在/WEB-INF/velocity/screen/下面找到目标vm文件,这里假设是demo.vm(/WEB-INF/velocity/screen/demo.vm).
2、启用引擎中的自定义宏库(去掉<property name="configLocation" value="classpath:velocity.properties" />的注释,上面日志对应的蓝色那段),其他配置不变,再次执行时,报错,说是找不到screen/demo.vm。日志如下:
2013-12-29 12:30:14,284 [DEBUG]----> Returning cached instance of singleton bean 'velocityConfigurer'
2013-12-29 12:30:14,286 [DEBUG]----> Invoking afterPropertiesSet() on bean with name 'demo.vm'
2013-12-29 12:30:14,287 [ERROR]----> ResourceManager : unable to find resource 'screen/demo.vm' in any resource loader.
2013-12-29 12:30:14,288 [DEBUG]----> No Velocity view found for URL: screen/demo.vm
=====问题一:1和2形成明显对比,怀疑是资源的类加载器不是同一个导致,但不明白原理,请指点。
第二组:
在视图处理器中,无论<property name="exposeSpringMacroHelpers" value="true" />中value是true还是false,debug日志打印中都显示加载spring.vm(上面日志绿色部分日志)和velocity.properties(jar包中自带那个,不是我放在类路径下的,上面日志红色部分日志)。
=====问题二:这是不是我的配置有问题?还是本身运行机制是这样,不影响执行呢?
感激不尽。
相关推荐
在Java企业级开发中,SpringMVC、Velocity和iBATIS是常见的三个组件,它们各自承担着不同的职责。SpringMVC作为一个强大的MVC框架,负责处理请求和响应,提供松耦合的架构;Velocity则是一个高效的模板引擎,用于...
SpringMVC 和 Velocity 的整合是将 Velocity 作为模板引擎与 SpringMVC 框架结合,以实现更高效、灵活的视图层渲染。Velocity 是一个开源的 Java 模板引擎,它允许开发者将业务逻辑与表现层内容分离,使得前端开发...
Spring、SpringMVC、Mybatis、Velocity和Maven是Java Web开发中常用的一组技术栈,它们各自在软件开发的不同层面发挥着重要作用。这个压缩包文件的标题和描述表明,它提供了一个集成这些技术的演示项目,下面我们将...
SpringMVC、Hibernate和Spring是Java开发中三大核心框架,它们各自负责应用程序的不同层面:SpringMVC用于处理HTTP请求和响应,Hibernate则是持久层框架,负责数据库操作,而Spring作为全能容器,提供依赖注入和面向...
在整合SpringMVC和Spring时,我们需要创建一个Spring的配置文件,通常命名为`applicationContext.xml`。这个文件定义了bean的配置,包括SpringMVC的DispatcherServlet配置、数据源、事务管理器、服务层接口及其实现...
在Java开发领域,Spring、SpringMVC和MyBatis是三个非常重要的开源框架,它们各自在不同的层次上解决了Web应用中的问题。Spring作为全面的框架,提供了依赖注入(DI)和面向切面编程(AOP)等核心特性;SpringMVC是...
在现代Java Web开发中,"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA"是一个常见的架构组合,被广泛应用于构建企业级应用程序。这个组合通常被称为"SSM",其中"M"代表Maven,"S"代表Spring,包括Spring核心...
在SpringMVC和Velocity的整合项目中,Maven可以帮助我们管理Spring、Velocity以及其它依赖库的版本,确保项目的可重复构建。 将SpringMVC、Velocity和Maven整合在一起,可以按照以下步骤进行: 1. **配置POM.xml**...
这个"SpringMVC + Spring + SpringJDBC整合demo"是一个完整的示例项目,旨在帮助开发者理解如何将这三个框架集成到一个应用中。 **SpringMVC**是Spring框架的一部分,专门用于构建Web应用程序。它提供了模型-视图-...
本文将深入探讨"Spring和SpringMVC整合"的相关知识点,以及如何创建一个简单的"Hello, World!"示例。 1. **Spring框架核心概念** - **依赖注入(Dependency Injection, DI)**:Spring的核心特性之一,通过DI,...
"Spring+SpringMVC+Mybatis框架项目整合"是一个典型的后端技术栈,旨在提供高效、灵活且可维护的解决方案。下面将详细介绍这三个框架及其整合过程中的关键知识点。 **Spring框架**: Spring是一个开源的Java平台,...
Spring MVC、MyBatis 和 Spring Security 是 Java Web 开发中常用的三大框架,它们各自负责不同的职责,但在实际项目中常被整合使用以构建强大的企业级应用。本整合 demo 展示了如何将这三个框架协同工作,提供高效...
标题 "mybatis3+spring+springMVC4整合jar包.rar" 描述的是一个整合了MyBatis 3、Spring 4 和 Spring MVC 4 的项目压缩包。这个压缩包通常用于快速搭建一个基于Java的Web开发环境,尤其是用于处理数据库操作和前端...
- 文件流式传输:避免一次性加载整个文件到内存,可以使用 Netty 的 ChunkedWriteHandler 支持大文件的流式传输。 - 异步处理:在 SpringMVC 中,可以利用 @Async 注解进行异步处理,提高服务性能。 - 错误处理:...
Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...
在Java Web开发中,Spring MVC和Hibernate是两个非常重要的框架,它们分别负责处理MVC(模型-视图-控制器)架构中的控制层和持久层。Spring MVC提供了强大的依赖注入、AOP(面向切面编程)等功能,而Hibernate则是一...
整合Jetty和SpringMVC通常包括以下几个步骤: - **创建Maven项目**:使用Maven作为构建工具,因为它提供了对依赖管理和项目构建的强大支持。 - **添加依赖**:在`pom.xml`中添加Jetty服务器和SpringMVC的依赖。 ...
Spring+Hibernate+SpringMVC+Velocity+Maven整合
SSM(Spring、SpringMVC和MyBatis)整合是Java Web开发中常见的技术栈,它结合了Spring框架的强大功能,SpringMVC的优雅请求处理,以及MyBatis的灵活数据库操作。本项目是一个入门级别的实例,旨在帮助初学者理解和...
**SpringMVC、Spring与SpringJDBC整合详解** 在Java Web开发中,Spring框架因其强大的功能和灵活性而被广泛使用。SpringMVC、Spring和SpringJDBC是Spring框架的三个重要组成部分,它们协同工作,提供了从用户接口到...