问题描述
我正在寻找一种对Freemarker上的字符串变量进行url解码的方法.
I'm looking for a way to url decode a string variable on Freemarker.
例如,考虑以下字符串:attr = hello + world%3F.预期结果:世界您好?
For example, consider the following string: attr=hello+world%3F .Expected result: hello world?
有什么简单的方法可以得到这个结果?有点像$ {attr?urlDecode}?我在网上找不到任何东西.
Any simple way to get this result? somewhat like ${attr?urlDecode}? I could not find anything online.
谢谢.
推荐答案
如上所述,FreeMarker仅内置了对URL编码的支持,而不是对解码的支持.但是,您可以通过创建自己的URI解码指令来解决此问题.
As mentioned above, FreeMarker only has built in support for URL encoding, not decoding. You can however solve this by creating your own directive for URI-decoding.
类似这样的内容: https://gist.github.com/lazee/285d206470fb6076d262
这里是一个小型测试类,向您展示如何将该指令添加为共享变量,以及如何在模板中使用它: https://gist.github.com/lazee/f5c524b3c7bebf68955e
Here is a small test class that shows you how you can add the directive as a shared variable, and how to use it in a template: https://gist.github.com/lazee/f5c524b3c7bebf68955e
这篇关于有什么办法可以在Freemarker上对变量进行url解码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!