Grails的响应方法使用charset = ISO-8859-1输出HTML内容

时间:2014-11-18 11:11:46

标签: grails

我刚刚注意到控制器中的respond方法正在返回ISO-8859-1字符集中的HTML响应(这会使我的unicode字符变得混乱)。如果我将格式设置为JSON,它使用UTF-8。 render方法也使用UTF-8。

我在开发模式下使用Grails 2.4.4和Tomcat插件v.7.0.55而没有覆盖web.xmlgrails.converters.encodinggrails.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']
]

1 个答案:

答案 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
}