问题
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