在我的Grails应用程序中,Unicode字符未被正确编码。我使用的是Grails 1.3.7和Tomcat 7.0.22。以下是我在应用程序中配置Unicode支持的设置:
- Set grails.views.gsp.encoding and grails.converters.encoding="UTF-8" in Config.groovy
- Set encoding to UTF-8 in meta tag in the .gsp pages
- Specified 'useUnicode=true&characterEncoding=UTF-8' to the MySql connection URL (DB has characterset set to UTF-8)
- Set URIEncoding="UTF-8" useBodyEncodingForURI="true" to the server.xml file in tomcat
- Specified the attribute accept-charset="UTF-8" of the form tag.
但是,在我提交Unicode字符时,Grails不支持该字符,并且会保存乱码值。我已经在谷歌上搜索并阅读了其他人对此问题的求助,但不幸的是这些解决方案对我没有用。尽管如此,我已经找到了解决这个问题的方法。以下是表达式:
params.detail = params.detail ? new String(params.detail.getBytes("8859_1"), "UTF8") : null
这个方法可以正确编码Unicode字符。
然而,使用这种方法很繁琐,因为我必须对应用程序中的所有文本输入执行此操作。为什么Grails和/或Tomcat不能正确编码Unicode字符?我认为我的设置是正确的。
curl --head %YOURSITE%
命令得到了什么结果? - Igor Artamonovparams.detail = params.detail ? new String(params.detail.getBytes("8859_1"), "UTF8") : null
可以正确地呈现Unicode字符。 - rstarter