在速度模板中创建对象 [英] Create object in velocity template

查看:93
本文介绍了在速度模板中创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的liferay主题编写速度模板,我想知道是否有可能在速度模板中创建一个新对象.

I am writing velocity templates for my liferay theme and I am wondering, whether it is possible to create a new object inside the velocity template.

原因是在liferay中没有在上下文中注册contextTool,我真的希望能够检查给定时间模板中存在的变量. 有一个很酷的宏,但不幸的是它使用了contexttool.

The reason is that in liferay there is no contextTool registered in the context and I really want to be able to inspect the variables that are present in the template at a given time. There is a cool macro for this, but unfortunately it uses the contexttool.

我想做类似的事情:

#set($contextTool = new ContextTool())

另一种解决方案是liferay主题提供的Java代码,该主题能够在模板上下文中添加内容.但我也不知道这怎么工作...;-)

Another solution would be java code that is provided with the liferay theme that is able to add stuff in the template context. But I don't know how this would work either... ;-)

推荐答案

尝试

#set($contextTool = $portal.getClass().forName("full.package.ContextTool").newInstance())

编辑

如果我了解您,那么这应该能给您想要的东西

IF I understood you than this should give you what you want

#set($ve = $serviceLocator.findService("com.liferay.portal.kernel.velocity.VelocityEngine"))
#set($wvc = $ve.getWrappedStandardToolsContext().getWrappedVelocityContext())

#set($cVE = $portal.getClass().forName("org.apache.velocity.app.VelocityEngine"))
#set($cHSREQ = $portal.getClass().forName("javax.servlet.http.HttpServletRequest"))
#set($cHSRES = $portal.getClass().forName("javax.servlet.http.HttpServletResponse"))
#set($cSC = $portal.getClass().forName("javax.servlet.ServletContext"))
#set($cCC = $portal.getClass().forName("org.apache.velocity.tools.view.context.ChainedContext"))
#set($cVEI = $portal.getClass().forName("com.liferay.portal.velocity.VelocityEngineImpl"))
#set($cC = $portal.getClass().forName("org.apache.velocity.context.Context"))
#set($cVEU = $portal.getClass().forName("com.liferay.portal.kernel.velocity.VelocityEngineUtil"))

#set($ve = $cVEU.getMethod("getVelocityEngine").invoke(null))

#set($fVE = $cVEI.getDeclaredField("_velocityEngine"))
$fVE.setAccessible(true)

#set($cc = $cCC.getConstructor($cC, $cVE, $cHSREQ, $cHSRES, $cSC).newInstance($wvc, $fVE.get($ve), $request, $response, $request.getSession().getServletContext()))

#set($contextTool = $portal.getClass().forName("org.apache.velocity.tools.view.tools.ContextTool").newInstance())

$contextTool.init($cc)

之后,您可以使用例如

$contextTool.getKeys()

如果这不是您所需要的,请告诉我...

If this is not what you need, let me know ...

这篇关于在速度模板中创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆