开发者

Grails - serve HTML files as UTF-8?

开发者 https://www.devze.com 2023-04-05 23:40 出处:网络
UTF-8 is broken on .html files served through tomcat through /web-app. If I open the file directly with file:///, it renders fine. If I view the file during run-app, it looks fine. However, when I\'m

UTF-8 is broken on .html files served through tomcat through /web-app. If I open the file directly with file:///, it renders fine. If I view the file during run-app, it looks fine. However, when I'm deployed as a war, UTF-8 characters appear garbled.

The content-type of the response seems correct... Content-Type: text/html;charset=UTF-8, and the HTML file itself even seems to have the correct meta declarations.

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

Tomcat's connector is set to "UTF-8" for开发者_运维技巧 default URIEncoding, so I don't think that's the issue either. GSPs are rendering fine; only the HTML file has an issue.

What could be going on here?

EDIT: Using firefox, I saved a local copy of the raw HTML file and the file read directly from file:///... for comparison. The only difference between the files is that the Tomcat version has all non-ascii characters replaced with this:

Which either renders as an empty square or question mark depending on what editor you're using. The character itself seems to be EF BF BD, which replaces all the non-ascii characters. Somehow, in serving a file, tomcat or grails is just stomping on the unicode bytes. What could do that?

EDIT EDIT: Even this w3 test file has the same behavior as my files, indicating that my files are probably fine, and there really is something up with Tomcat/Grails.


Today I currently faced this exact problem locally I was able to work fine but upon deploying with tomcat or glassfish all my files were not in the right encoding. A co-worker stumbled upon the solution.

​For the encoding to work properly when using grails with tomcat, you have to set the following tomcat options:

server.xml:
 <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8" />

catalina.sh:
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
-server -Xms2048m -Xmx2048m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"

Make sure to cntl-f5 when checking results to avoid cache


Are there any filter mappings before the charEncodingFilter in your web.xml? This is a common cause of pages getting rendered with the wrong encoding. charEncodingFilter should generally be the very first filter mapping in your web.xml. Most grails plugins that insert filter mappings have special logic to insert after this one.


Grails 1.3.7, at least, cannot serve HTML files correctly. Write your own file server controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号