apache 有没有办法在速度模板中对 URL 进行编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2520657/
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-09-13 18:57:13  来源:igfitidea点击:

Is there a way to encode a URL in velocity template

apachevelocity

提问by fermatthrm2

Excuse my ignorance but I am new to Velocity and trying to fix someone else's problem. I need to encode a URL inside the velocity template. I create a url and as part of the query string I pass in a page name a user created. This page can contain special characters like ?e?. The url would look like http://foo.com/page1/jz?page=SpecialChars_?e?

请原谅我的无知,但我是 Velocity 的新手并试图解决其他人的问题。我需要在速度模板中编码一个 URL。我创建了一个 url,作为查询字符串的一部分,我传入了一个用户创建的页面名称。此页面可以包含特殊字符,如 ?e?。网址看起来像http://foo.com/page1/jz?page=SpecialChars_?e?

回答by serg

To encode URL inside a template you can use:

要在模板中编码 URL,您可以使用:

$esc.url($myUrl)

$esc.url($myUrl)

which is a part of EscapeTool.

这是EscapeTool的一部分。

Note: This required to use velocity tools jar, in addition to the velocity jar. (It will not throw exception if you will not have it). Moreover, you might want to check you configuration, as describes here

注意:除了速度 jar 之外,这还需要使用速度工具jar 。(如果你没有它,它不会抛出异常)。此外,您可能想检查您的配置,如描述here

回答by sreeprasad

I know it is late. Here is how I solved this today. In the class calling the engine, you could say

我知道已经晚了。这是我今天解决这个问题的方法。在调用引擎的课堂上,你可以说

configure("esc",new EscapeTool());
context.put("url", "http://www.google.com");

Now in the template you could say

现在在模板中你可以说

$esc.url($url)

$esc.url($url)

回答by Prabhakar Singh

I was just not willing to use the EscapeTool in velocity for an url-encoding. Hence, here's the solution i got -

我只是不愿意在速度中使用 EscapeTool 进行 url 编码。因此,这是我得到的解决方案 -

you can use $httpUtil.decodeURL($siteURL)/ $httpUtil.encodeURL($siteURL)for URL Encoding in Velocity

您可以使用$httpUtil.decodeURL($siteURL)/ $httpUtil.encodeURL($siteURL)在 Velocity 中进行 URL 编码

Also ,you can use $htmlUtil.escapeAttribute()for escaping text/html Content in Velocity.

此外,您可以$htmlUtil.escapeAttribute()用于在 Velocity 中转义 text/html 内容。

$htmlUtil.escapeAttribute($refSiteName)

$htmlUtil.escapeAttribute($refSiteName)