我刚刚注意到控制器中的respond
方法正在返回ISO-8859-1字符集中的HTML响应(这会使我的unicode字符变得混乱)。如果我将格式设置为JSON,它使用UTF-8。 render
方法也使用UTF-8。
我在开发模式下使用Grails 2.4.4和Tomcat插件v.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"]
来强制使用charset,但它并没有改变任何内容。
有什么东西我没看到或者我遇到了什么错误?
编辑:我的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']
]
答案 0 :(得分:1)
这似乎是一个Grails错误,当发生这种情况时,我已经缩小了具体情况:你需要在控制器上设置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
}