java 使用反斜杠在速度中转义美元符号不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15547671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:00:53  来源:igfitidea点击:

Escaping the dollar sign in velocity using backslash not working

javavelocity

提问by Abinash Ktheitroadala

I have the following segment to get rendered from a velocity template file.

我有以下片段可以从速度模板文件中渲染。

xyz $$foo

The dollar signs need to be escaped from VTL parser. And in the template file I represented it as

美元符号需要从 VTL 解析器中转义。在模板文件中,我将其表示为

xyz $$foo

I expect

我预计

xyz $$foo

but I get

但我明白了

xyz $foo

What am I doing wrong??

我究竟做错了什么??

回答by Abinash Ktheitroadala

One of the way is to use a fixed string variable i.e, setting a variable for the $ sign and then using it.

一种方法是使用固定的字符串变量,即为 $ 符号设置一个变量,然后使用它。

#set ( $d = "$")
xyz ${d}${d}foo

It gives the expected output

它给出了预期的输出

xyz $$foo

回答by sonofrage

You might also want to check out the EscapeToolin velocity.

您可能还想查看EscapeTool的速度。

Once you have included it in context (e.g. under the name "esc"), you can use ${esc.d} to get the dollar sign. The method is you mentioned is easier for this particular case though.

一旦您将它包含在上下文中(例如,在名称“esc”下),您就可以使用 ${esc.d} 来获取美元符号。不过,您提到的方法对于这种特殊情况更容易。

I thought it was worth mentioning because it provides several other convenient methods to escape strings appropriate, if your templates are generating Java/javascript/html etc.

我认为值得一提,因为它提供了其他几种方便的方法来适当地转义字符串,如果您的模板正在生成 Java/javascript/html 等。

回答by jumpnett

It might also be helpful to use Velocity's NumberTool.

使用 Velocity 的NumberTool也可能会有所帮助。

It has a built in currency formatter:

它有一个内置的货币格式化程序:

$myNumber                   -> 13.55
$number.format($myNumber)   -> 13.6
$number.currency($myNumber) -> .55
$number.integer($myNumber)  -> 13