我刚刚注意到 Controller 中的respond
方法以ISO-8859-1字符集(使我的Unicode字符乱码)返回HTML响应。如果我将格式设置为JSON,它将使用UTF-8。 render
方法还使用UTF-8。
我在开发模式下使用Grails 2.4.4和Tomcat插件7.0.55版而不覆盖web.xml
。 grails.converters.encoding
和grails.views.gsp.encoding
都设置为UTF-8。我的模板中有<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
(如果有影响)。
我也尝试通过使用respond myInstance, [encoding: "UTF-8"]
来强制字符集,但是它没有任何改变。
有没有我看不到的东西,或者我遇到了错误?
编辑:我的config.groovy
包含以下mime类型定义:
grails.mime.types = [ // the first one is the default format
all: '*/*', // 'all' maps to '*' or the first available format in withFormat
atom: 'application/atom+xml',
css: 'text/css',
csv: 'text/csv',
form: 'application/x-www-form-urlencoded',
html: ['text/html','application/xhtml+xml'],
js: 'text/javascript',
json: ['application/json', 'text/json'],
multipartForm: 'multipart/form-data',
rss: 'application/rss+xml',
text: 'text/plain',
hal: ['application/hal+json','application/hal+xml'],
xml: ['text/xml', 'application/xml']
]
最佳答案
看来这是一个Grails错误,我已经缩小了发生这种情况的具体情况:您需要在 Controller 上具有static responseFormats = ['html', ...]
限制才能触发它。 Grails的generate-restful-controller
包含responseFormats
块的事实自动使开发人员更有可能遇到此问题。
我已经提交了bug report。
编辑:保留responseFormats
块,但仍然有UTF-8
响应,可以手动设置编码,也许像这样:
def beforeInterceptor = {
response.characterEncoding = 'UTF-8' //workaround for https://jira.grails.org/browse/GRAILS-11830
}
关于grails - Grails的响应方法以charset = ISO-8859-1输出HTML内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992902/