在Liferay Portal下spring MVC整合velocity

问题的具体详情和这位老外描述是一样的
http://forum.springsource.org/showthread.php?t=67257
即抛出异常
[quote]
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!
[/quote]

最后跟调试(关于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
代码下面有行小字
[quote]
Note
Fornonweb-appsadda VelocityConfigurationFactoryBean ora
FreeMarkerConfigurationFactoryBean toyourapplicationcontextdefinitionfile.
[/quote]

其中VelocityConfigurationFactoryBean 应该是写错了 没有这个类(FreeMarkerConfigurationFactoryBean )倒是有的
正确的应该是VelocityEngineFactoryBean


热烈欢迎对本文任何错误含糊之处斧正拍砖!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值