在 VelocityContext 中找不到值时,Velocity 可以为变量设置默认值吗? [英] can Velocity set a default value for a variable when no value found in VelocityContext?

查看:86
本文介绍了在 VelocityContext 中找不到值时,Velocity 可以为变量设置默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在 VelocityContext 中没有找到值,Velocity 只打印标签名称,即我的模板文件中的 $name,但在 VelocityContext 中没有name"的值,所以只打印了$name".如果变量没有值,我想让 Velocity 打印一个默认值,我只是尝试扩展 AbstractCotnext 并覆盖 internalGet() 方法,但是 internalGet() 的返回值将被强制转换为 Node 对象,我不知道如何在我的internalGet()方法中创建一个新的Node对象,而且我觉得这种方式很复杂.

Velocity just print the tag name if no value was found in VelocityContext, ie, $name in my template file, but there is no value for "name" in VelocityContext, so just "$name" was printed. I want Velocity to print a default value if there is no value for the variable, I just tried to extends AbstractCotnext and override internalGet() method, but the return value of internalGet() will be cast to Node object, I don't know how to create a new Node object in my internalGet() method, and also I think this way is very complex.

有没有设置默认值的简单方法(默认值只是一个字符串)?

is there a simple way to set a default value (default value is just a string) ?

谢谢.

推荐答案

就我所见,对于所有变量来说并不容易,我只设法对某些变量进行了处理,具体如下:

Not easily for all variables as far as I see, I only managed to do it for some variables specifically as follows:

模板:

#if ( !$somevar )
#set ( $somevar = "mycontent" )
#end

Var is: $somevar

结果:

Var is: mycontent

这篇关于在 VelocityContext 中找不到值时,Velocity 可以为变量设置默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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