Velocity以相对路径方式嵌入动态页面问题

初学Velocity,想写个VelocityTool类实现以相对路径方式嵌入动态页面,思路大致与http://www.oschina.net/code/snippet_12_694中一样,因为新版中init(Object arg0)方法已经Deprecated了,换用configure(Map props)(见http://velocity.apache.org/tools/devel/upgrading.html最下面和http://velocity.apache.org/tools/devel/creatingtools.html),代码如下:

	import java.util.Map;
	 
	import org.apache.velocity.VelocityContext;
	import org.apache.velocity.tools.view.ViewContext;
	 
	public class VelocityTool {
	 
	    protected VelocityContext velocity;
	 
	    public void configure(Map props) {
	        Object obj = props.get("velocityContext");
	        if (obj instanceof ViewContext) {
	            ViewContext viewContext = (ViewContext) obj;
	            velocity = (VelocityContext) viewContext.getVelocityContext();
	        }
	    }
	 
	    public String cur_templete() {
	        return velocity.getCurrentTemplateName();
	    }
	 
	}


我使用的Velocity和VelocityTools的版本都是最新的。

但是运行时出现错误:

Caused by: java.lang.ClassCastException: org.apache.velocity.tools.view.context.ChainedContext cannot be cast to org.apache.velocity.VelocityContext

可见从获得的ViewContext是ChainedContext类型了,不能强制转换为 VelocityContext类型。

我又换了老方法,使用init(Object arg0)方法,Copy红薯的代码来用,也不行,错误是一样的,都是ChainedContext不能强制转换为 VelocityContext。

各位高手,请问有办法解决吗?或者有其他办法可以达到这个目的吗?在使用最新版Velocity和VelocityTool的情况下。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧米优

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值