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

标签 grails

我刚刚注意到 Controller 中的respond方法以ISO-8859-1字符集(使我的Unicode字符乱码)返回HTML响应。如果我将格式设置为JSON,它将使用UTF-8。 render方法还使用UTF-8。

我在开发模式下使用Grails 2.4.4和Tomcat插件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"]来强制字符集,但是它没有任何改变。

有没有我看不到的东西,或者我遇到了错误?

编辑:我的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/

相关文章:

javascript - 在导入的 javascript 文件中使用 grails 变量

html - 从 JSON 数组读取到 Grails HTML Select 标签

email - Grails电子邮件确认插件的问题仅适用于Run War

grails - 如果已经在yml根目录中定义了Grails application.yml,则如何使用这些特定于环境的条目?

hibernate - 在Hibernate异常后可以使用Grails安全的方式执行更多查询吗?

搜索结果的 grails 分页

grails - 如何从 grails 默认 gsp 编码中排除插件?

grails - 使用 grails 更改 Spring 安全插件中的登录页面

mysql - Grails 数据库逆向工程失败

javascript - 一次 POST Grails 中的所有传入参数