问题的具体详情和这位老外描述是一样的
http://forum.springsource.org/showthread.php?t=67257
即抛出异常
引用
java.lang.IllegalStateException: WebApplicationObjectSupport instance [org.springframework.web.servlet.view.velocity.Velo cityView: name 'view'; URL [/WEB-INF/velocity/view.vm]] does not run within a ServletContext. Make sure the object is fully configured!
最后跟调试(关于Liferay远程调试Java Remote Debug和热部署HotDeploy的Eclipse开发环境配置有空再写上来)跟代码发现
if (servletContext == null && isContextRequired()) {
throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
"] does not run within a ServletContext. Make sure the object is fully configured!");
}
这里抛出的,
isContextRequired(),这个方法返回的是true(被VelocityView的父类AbstactTemplateView重写了)
一开始我以为getServletContext()==null 我一直以为是Liferay Portal的原因,所以就没考虑从这里解决问题.
我尝试写一个VelocityView的子类MyVelocityView,把isContextRequired()重写成return true;来尝试是否能恢复
但是重写后仍然调用VelocityView而不是MyVelocityView,开始怀疑是否是liferay自带的spring的加载顺序优先与自己项目的.(呃 具体说不清原理,感觉有这可能,有看客讲得清的欢迎斧正)
然后再看spring配置,突然发现没有配置Listener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/{你配置文件名}.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加上后问题解决了,因为这下getServletContext()!=null 了
==========回顾问题=================
为什么没有用velocity前,我的spring(IoC/MVC)作用正常呢
1.因为liferay会吧/WEB-INF/{你的portlet名称}-portlet.xml当作spring的Context加载进去
所以spring的运作正常
2.因为之前我用的是InternalResourceViewResolver,而它的isContextRequired() return false.
===========问题小总结================
记得web.xml里要配置好ContextLoaderListener
对于Portal来说ViewRendererServlet必须的,用来复用spring MVC的视图处理
=========篇外话===============
此外, 发现Spring-referrance中有一处小Typo错误哈
Chapter 16.4 Velocity & FreeMarker -- Contextconfiguration
代码下面有行小字
引用
Note
Fornonweb-appsadda VelocityConfigurationFactoryBean ora
FreeMarkerConfigurationFactoryBean toyourapplicationcontextdefinitionfile.
其中VelocityConfigurationFactoryBean 应该是写错了 没有这个类(FreeMarkerConfigurationFactoryBean )倒是有的
正确的应该是VelocityEngineFactoryBean
热烈欢迎对本文任何错误含糊之处斧正拍砖!