Escaping special characters in velocity template

浪尽此生 提交于 2021-02-08 04:57:07

问题


I have this template in vm:

<div>
    $submitterMessage
</div>

The variable $submitterMessage contains the special character like è,à,ò.

In my template the output of variable is: � � � �

How I can resolve it?

Thanks


回答1:


In that context you should apply HTML encoding/escaping. According to velocity a ready function is: http://velocity.apache.org/tools/devel/generic/EscapeTool.html#html%28%29

For a more complex context like: html attributes, nested javascript etc... you could use also ESAPI by OWASP or http://www.unbescape.org/. This will prevent also XSS vulnerabilities in your code.

Last but not least, you would probably want to work with a UTF-8 output; Set the right content type header in the http response and in the html headers in the page.




回答2:


Specifying the HTML5 charset as Tom Kriek suggested can be done directly in your template, in the <head> section:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

But then, you also have to tell Velocity that you're outputting UTF-8 characters. You just have to add the following to your velocity.properties file:

input.encoding = UTF8
output.encoding = UTF8

You'll only have to resort to the EscapeTool for outputting '<', '&,', '>', ...



来源:https://stackoverflow.com/questions/28543015/escaping-special-characters-in-velocity-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!